Remove obsolete reconnect callbacks
removes obsolete reconnect callbacks scheduled before network reconnects to prevent unwanted reconnection also renamed WebSocketConnection.onFailure to onNetworkFailure to clear confusion
This commit is contained in:
@@ -21,7 +21,8 @@ public class WebSocketConnection {
|
|||||||
private final ConnectivityManager connectivityManager;
|
private final ConnectivityManager connectivityManager;
|
||||||
private OkHttpClient client;
|
private OkHttpClient client;
|
||||||
|
|
||||||
private final Handler handler = new Handler();
|
private final Handler reconnectHandler = new Handler();
|
||||||
|
private Runnable reconnectCallback = this::start;
|
||||||
private int errorCount = 0;
|
private int errorCount = 0;
|
||||||
|
|
||||||
private final String baseUrl;
|
private final String baseUrl;
|
||||||
@@ -31,7 +32,7 @@ public class WebSocketConnection {
|
|||||||
private Runnable onClose;
|
private Runnable onClose;
|
||||||
private Runnable onOpen;
|
private Runnable onOpen;
|
||||||
private BadRequestRunnable onBadRequest;
|
private BadRequestRunnable onBadRequest;
|
||||||
private OnFailureCallback onFailure;
|
private OnNetworkFailureRunnable onNetworkFailure;
|
||||||
private Runnable onReconnected;
|
private Runnable onReconnected;
|
||||||
private boolean isClosed;
|
private boolean isClosed;
|
||||||
private Runnable onDisconnect;
|
private Runnable onDisconnect;
|
||||||
@@ -80,8 +81,8 @@ public class WebSocketConnection {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized WebSocketConnection onFailure(OnFailureCallback onFailure) {
|
synchronized WebSocketConnection onNetworkFailure(OnNetworkFailureRunnable onNetworkFailure) {
|
||||||
this.onFailure = onFailure;
|
this.onNetworkFailure = onNetworkFailure;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +119,18 @@ public class WebSocketConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void scheduleReconnect(long millis) {
|
||||||
|
reconnectHandler.removeCallbacks(reconnectCallback);
|
||||||
|
|
||||||
|
Log.i(
|
||||||
|
"WebSocket: scheduling a restart in "
|
||||||
|
+ TimeUnit.SECONDS.convert(millis, TimeUnit.MILLISECONDS)
|
||||||
|
+ " second(s)");
|
||||||
|
reconnectHandler.postDelayed(reconnectCallback, millis);
|
||||||
|
}
|
||||||
|
|
||||||
private class Listener extends WebSocketListener {
|
private class Listener extends WebSocketListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(WebSocket webSocket, Response response) {
|
public void onOpen(WebSocket webSocket, Response response) {
|
||||||
Log.i("WebSocket: opened");
|
Log.i("WebSocket: opened");
|
||||||
@@ -167,6 +179,8 @@ public class WebSocketConnection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
errorCount++;
|
||||||
|
|
||||||
NetworkInfo network = connectivityManager.getActiveNetworkInfo();
|
NetworkInfo network = connectivityManager.getActiveNetworkInfo();
|
||||||
if (network == null || !network.isConnected()) {
|
if (network == null || !network.isConnected()) {
|
||||||
Log.i("WebSocket: Network not connected");
|
Log.i("WebSocket: Network not connected");
|
||||||
@@ -174,14 +188,10 @@ public class WebSocketConnection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int minutes = Math.min(errorCount * 2 + 1, 20);
|
int minutes = Math.min(errorCount * 2 - 1, 20);
|
||||||
|
|
||||||
Log.i("WebSocket: trying to restart in " + minutes + " minute(s)");
|
onNetworkFailure.execute(minutes);
|
||||||
|
scheduleReconnect(TimeUnit.MINUTES.toMillis(minutes));
|
||||||
errorCount++;
|
|
||||||
handler.postDelayed(
|
|
||||||
WebSocketConnection.this::start, TimeUnit.MINUTES.toMillis(minutes));
|
|
||||||
onFailure.execute(minutes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
super.onFailure(webSocket, t, response);
|
super.onFailure(webSocket, t, response);
|
||||||
@@ -192,7 +202,7 @@ public class WebSocketConnection {
|
|||||||
void execute(String message);
|
void execute(String message);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OnFailureCallback {
|
interface OnNetworkFailureRunnable {
|
||||||
void execute(int minutesToTryAgain);
|
void execute(long millis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import android.content.IntentFilter;
|
|||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
@@ -97,7 +96,8 @@ public class WebSocketService extends Service {
|
|||||||
.onOpen(this::onOpen)
|
.onOpen(this::onOpen)
|
||||||
.onClose(() -> foreground(getString(R.string.websocket_closed)))
|
.onClose(() -> foreground(getString(R.string.websocket_closed)))
|
||||||
.onBadRequest(this::onBadRequest)
|
.onBadRequest(this::onBadRequest)
|
||||||
.onFailure((min) -> foreground(getString(R.string.websocket_failed, min)))
|
.onNetworkFailure(
|
||||||
|
(min) -> foreground(getString(R.string.websocket_failed, min)))
|
||||||
.onDisconnect(this::onDisconnect)
|
.onDisconnect(this::onDisconnect)
|
||||||
.onMessage(this::onMessage)
|
.onMessage(this::onMessage)
|
||||||
.onReconnected(this::notifyMissedNotifications)
|
.onReconnected(this::notifyMissedNotifications)
|
||||||
@@ -118,15 +118,7 @@ public class WebSocketService extends Service {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new Handler()
|
connection.scheduleReconnect(TimeUnit.SECONDS.toMillis(5));
|
||||||
.postDelayed(
|
|
||||||
() -> new Thread(this::notifyAndStart).start(),
|
|
||||||
TimeUnit.SECONDS.toMillis(5));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void notifyAndStart() {
|
|
||||||
notifyMissedNotifications();
|
|
||||||
connection.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onBadRequest(String message) {
|
private void onBadRequest(String message) {
|
||||||
|
|||||||
Reference in New Issue
Block a user