Commit 6fbbd3c1 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Check if server is a non connected state before continuing

parent 739efd88
...@@ -80,7 +80,7 @@ public class RocketChatService extends Service implements ConnectivityServiceInt ...@@ -80,7 +80,7 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
@Override @Override
public Single<Boolean> disconnectFromServer(String hostname) { //called via binder. public Single<Boolean> disconnectFromServer(String hostname) { //called via binder.
return Single.defer(() -> { return Single.defer(() -> {
if (!threadCreatedForHostname(hostname)) { if (!existsThreadForHostname(hostname)) {
return Single.just(true); return Single.just(true);
} }
...@@ -104,8 +104,8 @@ public class RocketChatService extends Service implements ConnectivityServiceInt ...@@ -104,8 +104,8 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
return Single.defer(() -> { return Single.defer(() -> {
webSocketThreadLock.acquire(); webSocketThreadLock.acquire();
int connectivityState = ConnectivityManager.getInstance(getApplicationContext()).getConnectivityState(hostname); int connectivityState = ConnectivityManager.getInstance(getApplicationContext()).getConnectivityState(hostname);
boolean isConnected = connectivityState == ServerConnectivity.STATE_CONNECTED; boolean isDisconnected = connectivityState != ServerConnectivity.STATE_CONNECTED;
if (currentWebSocketThread != null && threadCreatedForHostname(hostname)) { if (currentWebSocketThread != null && existsThreadForHostname(hostname) && !isDisconnected) {
webSocketThreadLock.release(); webSocketThreadLock.release();
return Single.just(currentWebSocketThread); return Single.just(currentWebSocketThread);
} }
...@@ -114,6 +114,7 @@ public class RocketChatService extends Service implements ConnectivityServiceInt ...@@ -114,6 +114,7 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
if (currentWebSocketThread != null) { if (currentWebSocketThread != null) {
return currentWebSocketThread.terminate() return currentWebSocketThread.terminate()
.doAfterTerminate(() -> currentWebSocketThread = null)
.doOnError(RCLog::e) .doOnError(RCLog::e)
.flatMap(terminated -> .flatMap(terminated ->
RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname) RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname)
...@@ -144,7 +145,7 @@ public class RocketChatService extends Service implements ConnectivityServiceInt ...@@ -144,7 +145,7 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
}); });
} }
private boolean threadCreatedForHostname(String hostname) { private boolean existsThreadForHostname(String hostname) {
if (hostname == null || currentWebSocketThread == null) { if (hostname == null || currentWebSocketThread == null) {
return false; return false;
} }
......
...@@ -320,10 +320,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -320,10 +320,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
error -> { error -> {
logErrorAndUnsubscribe(reconnectSubscription, error); logErrorAndUnsubscribe(reconnectSubscription, error);
connectivityManager.notifyConnectionLost(hostname, connectivityManager.notifyConnectionLost(hostname,
DDPClient.REASON_CLOSED_BY_USER); DDPClient.REASON_NETWORK_ERROR);
if (isAlive()) {
new Handler(getLooper()).post(this::unregisterListeners);
}
} }
) )
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment