Permalink
Browse files

connect / disconnect with websocket not when starting / stopping

Reviewed By: kittens

Differential Revision: D4937181

fbshipit-source-id: d1ec8144567809537fc7e321c255501bb130939b
  • Loading branch information...
emilsjolander authored and facebook-github-bot committed Apr 24, 2017
1 parent f09fdcb commit f1d5fdd46842daaebb90b25ebc2b0c3ed13d9fd2
@@ -76,7 +76,7 @@ public JSPackagerClient(String clientId, PackagerConnectionSettings settings, Ma
.appendQueryParameter("clientid", clientId);
String url = builder.build().toString();
mWebSocket = new ReconnectingWebSocket(url, this);
mWebSocket = new ReconnectingWebSocket(url, this, null);
mRequestHandlers = requestHandlers;
}
@@ -41,17 +41,27 @@
void onMessage(ResponseBody message);
}
public interface ConnectionCallback {
void onConnected();
void onDisconnected();
}
private final String mUrl;
private final Handler mHandler;
private boolean mClosed = false;
private boolean mSuppressConnectionErrors;
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();
mUrl = url;
mCallback = callback;
mMessageCallback = messageCallback;
mConnectionCallback = connectionCallback;
mHandler = new Handler(Looper.getMainLooper());
}
@@ -101,7 +111,11 @@ public void run() {
public void closeQuietly() {
mClosed = true;
closeWebSocketQuietly();
mCallback = null;
mMessageCallback = null;
if (mConnectionCallback != null) {
mConnectionCallback.onConnected();

This comment has been minimized.

Show comment
Hide comment
@baoti

baoti Apr 24, 2017

onDisconnected() ?

@baoti

baoti Apr 24, 2017

onDisconnected() ?

This comment has been minimized.

Show comment
Hide comment
@ptomasroos
@ptomasroos

This comment has been minimized.

Show comment
Hide comment
@emilsjolander

emilsjolander Apr 26, 2017

Contributor

Oops, typo! Thanks for noticing 👍

@emilsjolander

emilsjolander Apr 26, 2017

Contributor

Oops, typo! Thanks for noticing 👍

This comment has been minimized.

Show comment
Hide comment
@baoti

baoti Apr 26, 2017

think about mConnectionCallback = null

@baoti

baoti Apr 26, 2017

think about mConnectionCallback = null

}
}
private void closeWebSocketQuietly() {
@@ -124,6 +138,10 @@ private void abort(String message, Throwable cause) {
public synchronized void onOpen(WebSocket webSocket, Response response) {
mWebSocket = webSocket;
mSuppressConnectionErrors = false;
if (mConnectionCallback != null) {
mConnectionCallback.onConnected();
}
}
@Override
@@ -132,14 +150,17 @@ public synchronized void onFailure(IOException e, Response response) {
abort("Websocket exception", e);
}
if (!mClosed) {
if (mConnectionCallback != null) {
mConnectionCallback.onDisconnected();
}
reconnect();
}
}
@Override
public synchronized void onMessage(ResponseBody message) {
if (mCallback != null) {
mCallback.onMessage(message);
if (mMessageCallback != null) {
mMessageCallback.onMessage(message);
}
}
@@ -150,6 +171,9 @@ public synchronized void onPong(Buffer payload) { }
public synchronized void onClose(int code, String reason) {
mWebSocket = null;
if (!mClosed) {
if (mConnectionCallback != null) {
mConnectionCallback.onDisconnected();
}
reconnect();
}
}

0 comments on commit f1d5fdd

Please sign in to comment.