Wait for network when websocket connection failed
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.github.gotify.service;
|
||||
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Handler;
|
||||
import com.github.gotify.SSLSettings;
|
||||
import com.github.gotify.Utils;
|
||||
@@ -16,6 +18,7 @@ import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
|
||||
public class WebSocketConnection {
|
||||
private final ConnectivityManager connectivityManager;
|
||||
private OkHttpClient client;
|
||||
|
||||
private final Handler handler = new Handler();
|
||||
@@ -31,8 +34,14 @@ public class WebSocketConnection {
|
||||
private OnFailureCallback onFailure;
|
||||
private Runnable onReconnected;
|
||||
private boolean isClosed;
|
||||
private Runnable onDisconnect;
|
||||
|
||||
WebSocketConnection(String baseUrl, SSLSettings settings, String token) {
|
||||
WebSocketConnection(
|
||||
String baseUrl,
|
||||
SSLSettings settings,
|
||||
String token,
|
||||
ConnectivityManager connectivityManager) {
|
||||
this.connectivityManager = connectivityManager;
|
||||
OkHttpClient.Builder builder =
|
||||
new OkHttpClient.Builder()
|
||||
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||
@@ -66,6 +75,11 @@ public class WebSocketConnection {
|
||||
return this;
|
||||
}
|
||||
|
||||
synchronized WebSocketConnection onDisconnect(Runnable onDisconnect) {
|
||||
this.onDisconnect = onDisconnect;
|
||||
return this;
|
||||
}
|
||||
|
||||
synchronized WebSocketConnection onFailure(OnFailureCallback onFailure) {
|
||||
this.onFailure = onFailure;
|
||||
return this;
|
||||
@@ -153,6 +167,13 @@ public class WebSocketConnection {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkInfo network = connectivityManager.getActiveNetworkInfo();
|
||||
if (network == null || !network.isConnected()) {
|
||||
Log.i("WebSocket: Network not connected");
|
||||
onDisconnect.run();
|
||||
return;
|
||||
}
|
||||
|
||||
int minutes = Math.min(errorCount * 2 + 1, 20);
|
||||
|
||||
Log.i("WebSocket: trying to restart in " + minutes + " minute(s)");
|
||||
|
||||
Reference in New Issue
Block a user