mirror of
https://github.com/status-im/react-native.git
synced 2025-02-26 16:10:58 +00:00
Make packager reconnect less noisy
Reviewed By: frantic Differential Revision: D3509012 fbshipit-source-id: 66742ebed80ecf48ce8291b1816ef0ec672febee
This commit is contained in:
parent
6779d13dcb
commit
f11a783e64
@ -11,10 +11,11 @@ package com.facebook.react.bridge;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
import com.facebook.common.logging.FLog;
|
import com.facebook.common.logging.FLog;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonFactory;
|
import com.fasterxml.jackson.core.JsonFactory;
|
||||||
@ -33,9 +34,13 @@ import okio.Buffer;
|
|||||||
* A wrapper around WebSocketClient that recognizes packager's message format.
|
* A wrapper around WebSocketClient that recognizes packager's message format.
|
||||||
*/
|
*/
|
||||||
public class JSPackagerWebSocketClient implements WebSocketListener {
|
public class JSPackagerWebSocketClient implements WebSocketListener {
|
||||||
|
|
||||||
private static final String TAG = "JSPackagerWebSocketClient";
|
private static final String TAG = "JSPackagerWebSocketClient";
|
||||||
|
|
||||||
|
private static final int RECONNECT_DELAY_MS = 2000;
|
||||||
|
|
||||||
private final String mUrl;
|
private final String mUrl;
|
||||||
|
private final Handler mHandler;
|
||||||
|
private boolean mSuppressConnectionErrors;
|
||||||
|
|
||||||
public interface JSPackagerCallback {
|
public interface JSPackagerCallback {
|
||||||
void onMessage(String target, String action);
|
void onMessage(String target, String action);
|
||||||
@ -48,6 +53,7 @@ public class JSPackagerWebSocketClient implements WebSocketListener {
|
|||||||
super();
|
super();
|
||||||
mUrl = url;
|
mUrl = url;
|
||||||
mCallback = callback;
|
mCallback = callback;
|
||||||
|
mHandler = new Handler(Looper.getMainLooper());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect() {
|
public void connect() {
|
||||||
@ -63,12 +69,17 @@ public class JSPackagerWebSocketClient implements WebSocketListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void reconnect() {
|
private void reconnect() {
|
||||||
new Timer().schedule(new TimerTask() {
|
if (!mSuppressConnectionErrors) {
|
||||||
@Override
|
FLog.w(TAG, "Couldn't connect to packager, will silently retry");
|
||||||
public void run() {
|
mSuppressConnectionErrors = true;
|
||||||
connect();
|
}
|
||||||
}
|
mHandler.postDelayed(
|
||||||
}, 2000);
|
new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
}, RECONNECT_DELAY_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void closeQuietly() {
|
public void closeQuietly() {
|
||||||
@ -137,13 +148,16 @@ public class JSPackagerWebSocketClient implements WebSocketListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(IOException e, Response response) {
|
public void onFailure(IOException e, Response response) {
|
||||||
abort("Websocket exception", e);
|
if (mWebSocket != null) {
|
||||||
|
abort("Websocket exception", e);
|
||||||
|
}
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(WebSocket webSocket, Response response) {
|
public void onOpen(WebSocket webSocket, Response response) {
|
||||||
mWebSocket = webSocket;
|
mWebSocket = webSocket;
|
||||||
|
mSuppressConnectionErrors = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user