Commit 5535e0ad authored by Leonardo Aramaki's avatar Leonardo Aramaki

Keep a single socket connected

parent c85bbb84
...@@ -3,19 +3,20 @@ package chat.rocket.android_ddp; ...@@ -3,19 +3,20 @@ package chat.rocket.android_ddp;
import android.text.TextUtils; import android.text.TextUtils;
import chat.rocket.android.log.RCLog;
import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import bolts.Task; import bolts.Task;
import bolts.TaskCompletionSource; import bolts.TaskCompletionSource;
import chat.rocket.android.log.RCLog;
import chat.rocket.android_ddp.rx.RxWebSocketCallback; import chat.rocket.android_ddp.rx.RxWebSocketCallback;
import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
public class DDPClient { public class DDPClient {
...@@ -24,6 +25,7 @@ public class DDPClient { ...@@ -24,6 +25,7 @@ public class DDPClient {
private static volatile DDPClient singleton; private static volatile DDPClient singleton;
private static OkHttpClient client; private static OkHttpClient client;
private final DDPClientImpl impl; private final DDPClientImpl impl;
private final AtomicReference<String> hostname = new AtomicReference<>();
public static void initialize(OkHttpClient okHttpClient) { public static void initialize(OkHttpClient okHttpClient) {
client = okHttpClient; client = okHttpClient;
...@@ -47,6 +49,11 @@ public class DDPClient { ...@@ -47,6 +49,11 @@ public class DDPClient {
} }
public Task<DDPClientCallback.Connect> connect(String url, String session) { public Task<DDPClientCallback.Connect> connect(String url, String session) {
String oldHostname = hostname.get();
hostname.set(url);
if (oldHostname != null && !url.equalsIgnoreCase(oldHostname)) {
close();
}
TaskCompletionSource<DDPClientCallback.Connect> task = new TaskCompletionSource<>(); TaskCompletionSource<DDPClientCallback.Connect> task = new TaskCompletionSource<>();
impl.connect(task, url, session); impl.connect(task, url, session);
return task.getTask(); return task.getTask();
......
...@@ -103,7 +103,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -103,7 +103,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
} }
if (assertServerRealmStoreExists(newHostname)) { if (assertServerRealmStoreExists(newHostname)) {
updateHostname(newHostname); recreate();
} else { } else {
recoverFromHostnameError(); recoverFromHostnameError();
} }
......
...@@ -67,11 +67,16 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -67,11 +67,16 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if (hostname == null || presenter == null) { if (hostname == null || presenter == null) {
String previousHostname = hostname;
hostname = new RocketChatCache(getApplicationContext()).getSelectedServerHostname(); hostname = new RocketChatCache(getApplicationContext()).getSelectedServerHostname();
if (hostname == null) { if (hostname == null) {
showAddServerScreen(); showAddServerScreen();
} else { } else {
onHostnameUpdated(); onHostnameUpdated();
if (!hostname.equalsIgnoreCase(previousHostname)) {
ConnectivityManager.getInstance(getApplicationContext()).resetConnectivityStateList();
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer();
}
} }
} else { } else {
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer(); ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer();
......
...@@ -25,4 +25,6 @@ public interface ConnectivityManagerApi { ...@@ -25,4 +25,6 @@ public interface ConnectivityManagerApi {
Observable<ServerConnectivity> getServerConnectivityAsObservable(); Observable<ServerConnectivity> getServerConnectivityAsObservable();
int getConnectivityState(@NonNull String hostname); int getConnectivityState(@NonNull String hostname);
void resetConnectivityStateList();
} }
...@@ -13,6 +13,7 @@ import java.util.Map; ...@@ -13,6 +13,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.helper.RxHelper; import chat.rocket.android.helper.RxHelper;
import chat.rocket.android.log.RCLog; import chat.rocket.android.log.RCLog;
import chat.rocket.core.models.ServerInfo; import chat.rocket.core.models.ServerInfo;
...@@ -68,11 +69,10 @@ import rx.subjects.PublishSubject; ...@@ -68,11 +69,10 @@ import rx.subjects.PublishSubject;
@DebugLog @DebugLog
@Override @Override
public void ensureConnections() { public void ensureConnections() {
for (String hostname : serverConnectivityList.keySet()) { String hostname = new RocketChatCache(appContext).getSelectedServerHostname();
connectToServerIfNeeded(hostname, true/* force connect */) connectToServerIfNeeded(hostname, true/* force connect */)
.subscribe(_val -> { .subscribe(_val -> {
}, RCLog::e); }, RCLog::e);
}
} }
@Override @Override
......
...@@ -263,7 +263,9 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -263,7 +263,9 @@ public class RocketChatWebSocketThread extends HandlerThread {
// handling WebSocket#onClose() callback. // handling WebSocket#onClose() callback.
task.getResult().client.getOnCloseCallback().onSuccess(_task -> { task.getResult().client.getOnCloseCallback().onSuccess(_task -> {
reconnect(); if (_task.getResult().code != 1000) {
reconnect();
}
return null; return null;
}); });
......
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