automatically re-establish websocket connections

This commit is contained in:
Akinwale Ariwodola 2020-09-09 14:15:31 +01:00
parent 5edbe763b1
commit e4b47631bd

View file

@ -212,6 +212,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
private static final int REMOTE_NOTIFICATION_REFRESH_TTL = 300000; // 5 minutes private static final int REMOTE_NOTIFICATION_REFRESH_TTL = 300000; // 5 minutes
public static final String SKU_SKIP = "lbryskip"; public static final String SKU_SKIP = "lbryskip";
private boolean shuttingDown;
private Date remoteNotifcationsLastLoaded; private Date remoteNotifcationsLastLoaded;
private Map<String, Class> specialRouteFragmentClassMap; private Map<String, Class> specialRouteFragmentClassMap;
@Getter @Getter
@ -1007,6 +1008,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
@Override @Override
protected void onDestroy() { protected void onDestroy() {
shuttingDown = true;
unregisterReceivers(); unregisterReceivers();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (receivedStopService || !isServiceRunning(this, LbrynetService.class)) { if (receivedStopService || !isServiceRunning(this, LbrynetService.class)) {
@ -1087,9 +1089,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
if ((webSocketClient == null || webSocketClient.isClosed()) && !Helper.isNullOrEmpty(Lbryio.AUTH_TOKEN)) { if ((webSocketClient == null || webSocketClient.isClosed()) && !Helper.isNullOrEmpty(Lbryio.AUTH_TOKEN)) {
webSocketClient = new WebSocketClient(new URI(String.format("%s%s", Lbryio.WS_CONNECTION_BASE_URL, Lbryio.AUTH_TOKEN))) { webSocketClient = new WebSocketClient(new URI(String.format("%s%s", Lbryio.WS_CONNECTION_BASE_URL, Lbryio.AUTH_TOKEN))) {
@Override @Override
public void onOpen(ServerHandshake handshakedata) { public void onOpen(ServerHandshake handshakedata) { }
}
@Override @Override
public void onMessage(String message) { public void onMessage(String message) {
@ -1098,13 +1098,14 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
@Override @Override
public void onClose(int code, String reason, boolean remote) { public void onClose(int code, String reason, boolean remote) {
if (!shuttingDown) {
// attempt to re-establish the connection if the app isn't being closed
checkWebSocketClient();
}
} }
@Override @Override
public void onError(Exception ex) { public void onError(Exception ex) { }
}
}; };
webSocketClient.connect(); webSocketClient.connect();
} }