Reconnect on websocket close

This commit is contained in:
Jannis Mattheis
2020-08-01 14:10:29 +02:00
parent 31c96e2d15
commit 8f045e20f3
2 changed files with 23 additions and 2 deletions

View File

@@ -35,6 +35,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import static com.github.gotify.api.Callback.call;
public class WebSocketService extends Service { public class WebSocketService extends Service {
public static final String NEW_MESSAGE_BROADCAST = public static final String NEW_MESSAGE_BROADCAST =
@@ -105,7 +107,7 @@ public class WebSocketService extends Service {
cm, cm,
alarmManager) alarmManager)
.onOpen(this::onOpen) .onOpen(this::onOpen)
.onClose(() -> foreground(getString(R.string.websocket_closed))) .onClose(this::onClose)
.onBadRequest(this::onBadRequest) .onBadRequest(this::onBadRequest)
.onNetworkFailure( .onNetworkFailure(
(min) -> foreground(getString(R.string.websocket_failed, min))) (min) -> foreground(getString(R.string.websocket_failed, min)))
@@ -122,6 +124,24 @@ public class WebSocketService extends Service {
picassoHandler.updateAppIds(); picassoHandler.updateAppIds();
} }
private void onClose() {
foreground(getString(R.string.websocket_closed_try_reconnect));
ClientFactory.userApiWithToken(settings)
.currentUser()
.enqueue(
call(
(ignored) -> this.doReconnect(),
(exception) -> {
if (exception.code() == 401) {
foreground(getString(R.string.websocket_closed_logout));
} else {
Log.i(
"WebSocket closed but the user still authenticated, trying to reconnect");
this.doReconnect();
}
}));
}
private void onDisconnect() { private void onDisconnect() {
foreground(getString(R.string.websocket_no_network)); foreground(getString(R.string.websocket_no_network));
} }

View File

@@ -21,7 +21,8 @@
<string name="auth_failed">Server returned 401 unauthorized while trying to get current user. This can be caused by deleting the client for this session.</string> <string name="auth_failed">Server returned 401 unauthorized while trying to get current user. This can be caused by deleting the client for this session.</string>
<string name="other_error">Could not get current user. Server (%s) responded with code %d: body (first 200 chars): %s</string> <string name="other_error">Could not get current user. Server (%s) responded with code %d: body (first 200 chars): %s</string>
<string name="websocket_failed">Connection failed, trying again in %d minutes</string> <string name="websocket_failed">Connection failed, trying again in %d minutes</string>
<string name="websocket_closed">WebSocket closed; The client-token could be invalidated, please re-login</string> <string name="websocket_closed_logout">User action required: Please login, the authentication token isn\'t valid anymore.</string>
<string name="websocket_closed_try_reconnect">Connection closed, trying to establish a new one.</string>
<string name="grouped_message">Received %d messages while being disconnected</string> <string name="grouped_message">Received %d messages while being disconnected</string>
<string name="delete_all">Delete all</string> <string name="delete_all">Delete all</string>
<string name="delete_logs">Delete logs</string> <string name="delete_logs">Delete logs</string>