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