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.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));
}