Reconnect on websocket close
This commit is contained in:
@@ -35,6 +35,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static com.github.gotify.api.Callback.call;
|
||||
|
||||
public class WebSocketService extends Service {
|
||||
|
||||
public static final String NEW_MESSAGE_BROADCAST =
|
||||
@@ -105,7 +107,7 @@ public class WebSocketService extends Service {
|
||||
cm,
|
||||
alarmManager)
|
||||
.onOpen(this::onOpen)
|
||||
.onClose(() -> foreground(getString(R.string.websocket_closed)))
|
||||
.onClose(this::onClose)
|
||||
.onBadRequest(this::onBadRequest)
|
||||
.onNetworkFailure(
|
||||
(min) -> foreground(getString(R.string.websocket_failed, min)))
|
||||
@@ -122,6 +124,24 @@ public class WebSocketService extends Service {
|
||||
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() {
|
||||
foreground(getString(R.string.websocket_no_network));
|
||||
}
|
||||
|
||||
@@ -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="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_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="delete_all">Delete all</string>
|
||||
<string name="delete_logs">Delete logs</string>
|
||||
|
||||
Reference in New Issue
Block a user