connect / disconnect with websocket not when starting / stopping

Reviewed By: kittens

Differential Revision: D4937181

fbshipit-source-id: d1ec8144567809537fc7e321c255501bb130939b
This commit is contained in:
Emil Sjolander 2017-04-24 03:53:46 -07:00 committed by Facebook Github Bot
parent f09fdcbd2b
commit f1d5fdd468
2 changed files with 31 additions and 7 deletions

View File

@ -76,7 +76,7 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
.appendQueryParameter("clientid", clientId); .appendQueryParameter("clientid", clientId);
String url = builder.build().toString(); String url = builder.build().toString();
mWebSocket = new ReconnectingWebSocket(url, this); mWebSocket = new ReconnectingWebSocket(url, this, null);
mRequestHandlers = requestHandlers; mRequestHandlers = requestHandlers;
} }

View File

@ -41,17 +41,27 @@ final public class ReconnectingWebSocket implements WebSocketListener {
void onMessage(ResponseBody message); void onMessage(ResponseBody message);
} }
public interface ConnectionCallback {
void onConnected();
void onDisconnected();
}
private final String mUrl; private final String mUrl;
private final Handler mHandler; private final Handler mHandler;
private boolean mClosed = false; private boolean mClosed = false;
private boolean mSuppressConnectionErrors; private boolean mSuppressConnectionErrors;
private @Nullable WebSocket mWebSocket; private @Nullable WebSocket mWebSocket;
private @Nullable MessageCallback mCallback; private @Nullable MessageCallback mMessageCallback;
private @Nullable ConnectionCallback mConnectionCallback;
public ReconnectingWebSocket(String url, MessageCallback callback) { public ReconnectingWebSocket(
String url,
MessageCallback messageCallback,
ConnectionCallback connectionCallback) {
super(); super();
mUrl = url; mUrl = url;
mCallback = callback; mMessageCallback = messageCallback;
mConnectionCallback = connectionCallback;
mHandler = new Handler(Looper.getMainLooper()); mHandler = new Handler(Looper.getMainLooper());
} }
@ -101,7 +111,11 @@ final public class ReconnectingWebSocket implements WebSocketListener {
public void closeQuietly() { public void closeQuietly() {
mClosed = true; mClosed = true;
closeWebSocketQuietly(); closeWebSocketQuietly();
mCallback = null; mMessageCallback = null;
if (mConnectionCallback != null) {
mConnectionCallback.onConnected();
}
} }
private void closeWebSocketQuietly() { private void closeWebSocketQuietly() {
@ -124,6 +138,10 @@ final public class ReconnectingWebSocket implements WebSocketListener {
public synchronized void onOpen(WebSocket webSocket, Response response) { public synchronized void onOpen(WebSocket webSocket, Response response) {
mWebSocket = webSocket; mWebSocket = webSocket;
mSuppressConnectionErrors = false; mSuppressConnectionErrors = false;
if (mConnectionCallback != null) {
mConnectionCallback.onConnected();
}
} }
@Override @Override
@ -132,14 +150,17 @@ final public class ReconnectingWebSocket implements WebSocketListener {
abort("Websocket exception", e); abort("Websocket exception", e);
} }
if (!mClosed) { if (!mClosed) {
if (mConnectionCallback != null) {
mConnectionCallback.onDisconnected();
}
reconnect(); reconnect();
} }
} }
@Override @Override
public synchronized void onMessage(ResponseBody message) { public synchronized void onMessage(ResponseBody message) {
if (mCallback != null) { if (mMessageCallback != null) {
mCallback.onMessage(message); mMessageCallback.onMessage(message);
} }
} }
@ -150,6 +171,9 @@ final public class ReconnectingWebSocket implements WebSocketListener {
public synchronized void onClose(int code, String reason) { public synchronized void onClose(int code, String reason) {
mWebSocket = null; mWebSocket = null;
if (!mClosed) { if (!mClosed) {
if (mConnectionCallback != null) {
mConnectionCallback.onDisconnected();
}
reconnect(); reconnect();
} }
} }