diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java index ded3ba9ec..45363bb5f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java @@ -35,7 +35,6 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; -import okhttp3.ws.WebSocket; import okio.Okio; import okio.Sink; @@ -85,7 +84,7 @@ public class DevServerHelper { public interface PackagerCommandListener { void onPackagerReloadCommand(); void onCaptureHeapCommand(); - void onPokeSamplingProfilerCommand(@Nullable final WebSocket webSocket); + void onPokeSamplingProfilerCommand(@Nullable final JSPackagerWebSocketClient.WebSocketSender webSocket); } public interface PackagerStatusCallback { @@ -125,7 +124,10 @@ public class DevServerHelper { mPackagerConnection = new JSPackagerWebSocketClient(getPackagerConnectionURL(), new JSPackagerWebSocketClient.JSPackagerCallback() { @Override - public void onMessage(@Nullable WebSocket webSocket, String target, String action) { + public void onMessage( + @Nullable JSPackagerWebSocketClient.WebSocketSender webSocket, + String target, + String action) { if (commandListener != null && "bridge".equals(target)) { if ("reload".equals(action)) { commandListener.onPackagerReloadCommand(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java index 40adde916..6097bb351 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java @@ -689,7 +689,8 @@ public class DevSupportManagerImpl implements } @Override - public void onPokeSamplingProfilerCommand(@Nullable final WebSocket webSocket) { + public void onPokeSamplingProfilerCommand( + @Nullable final JSPackagerWebSocketClient.WebSocketSender webSocket) { UiThreadUtil.runOnUiThread(new Runnable() { @Override public void run() { @@ -704,7 +705,8 @@ public class DevSupportManagerImpl implements JSCHeapUpload.captureCallback(mDevServerHelper.getHeapCaptureUploadUrl())); } - private void handlePokeSamplingProfiler(@Nullable WebSocket webSocket) { + private void handlePokeSamplingProfiler( + @Nullable JSPackagerWebSocketClient.WebSocketSender webSocket) { try { List pokeResults = JSCSamplingProfiler.poke(60000); for (String result : pokeResults) { @@ -716,7 +718,12 @@ public class DevSupportManagerImpl implements Toast.LENGTH_LONG).show(); if (webSocket != null) { // WebSocket is provided, so there is a client waiting our response - webSocket.sendMessage(RequestBody.create(WebSocket.TEXT, result == null ? "" : result)); + webSocket.sendMessage( + RequestBody.create( + WebSocket.TEXT, + result == null + ? "{\"target\":\"profiler\", \"action\":\"started\"}" + : result)); } else if (result != null) { // The profile was not initiated by external client, so process the // profile if there is one in the result diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSPackagerWebSocketClient.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSPackagerWebSocketClient.java index 97a7e5e4d..63764b790 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSPackagerWebSocketClient.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSPackagerWebSocketClient.java @@ -22,6 +22,7 @@ import com.facebook.common.logging.FLog; import okhttp3.OkHttpClient; import okhttp3.Request; +import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import okhttp3.ws.WebSocket; @@ -42,8 +43,20 @@ public class JSPackagerWebSocketClient implements WebSocketListener { private boolean mClosed = false; private boolean mSuppressConnectionErrors; + final public class WebSocketSender { + private WebSocket mWebSocket; + + public WebSocketSender(WebSocket webSocket) { + mWebSocket = webSocket; + } + + public void sendMessage(RequestBody requestBody) throws IOException { + mWebSocket.sendMessage(requestBody); + } + } + public interface JSPackagerCallback { - void onMessage(@Nullable WebSocket webSocket, String target, String action); + void onMessage(@Nullable WebSocketSender webSocket, String target, String action); } private @Nullable WebSocket mWebSocket; @@ -110,7 +123,10 @@ public class JSPackagerWebSocketClient implements WebSocketListener { private void triggerMessageCallback(String target, String action) { if (mCallback != null) { - mCallback.onMessage(mWebSocket, target, action); + WebSocketSender webSocketSender = mWebSocket == null + ? null + : new WebSocketSender(mWebSocket); + mCallback.onMessage(webSocketSender, target, action); } } diff --git a/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSPackagerWebSocketClientTest.java b/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSPackagerWebSocketClientTest.java index 4cb40ce72..f3d40e4a7 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSPackagerWebSocketClientTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSPackagerWebSocketClientTest.java @@ -34,7 +34,10 @@ public class JSPackagerWebSocketClientTest { final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback); client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"version\": 1, \"target\": \"targetValue\", \"action\": \"actionValue\"}")); - verify(callback).onMessage(any(WebSocket.class), eq("targetValue"), eq("actionValue")); + verify(callback).onMessage( + any(JSPackagerWebSocketClient.WebSocketSender.class), + eq("targetValue"), + eq("actionValue")); } @Test @@ -44,7 +47,10 @@ public class JSPackagerWebSocketClientTest { final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback); client.onMessage(ResponseBody.create(WebSocket.BINARY, "{\"version\": 1, \"target\": \"targetValue\", \"action\": \"actionValue\"}")); - verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString()); + verify(callback, never()).onMessage( + any(JSPackagerWebSocketClient.WebSocketSender.class), + anyString(), + anyString()); } @Test @@ -54,7 +60,10 @@ public class JSPackagerWebSocketClientTest { final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback); client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"version\": 1, \"action\": \"actionValue\"}")); - verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString()); + verify(callback, never()).onMessage( + any(JSPackagerWebSocketClient.WebSocketSender.class), + anyString(), + anyString()); } @Test @@ -64,7 +73,10 @@ public class JSPackagerWebSocketClientTest { final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback); client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"version\": 1, \"target\": null, \"action\": \"actionValue\"}")); - verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString()); + verify(callback, never()).onMessage( + any(JSPackagerWebSocketClient.WebSocketSender.class), + anyString(), + anyString()); } @Test @@ -74,7 +86,10 @@ public class JSPackagerWebSocketClientTest { final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback); client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"version\": 1, \"target\": \"targetValue\"}")); - verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString()); + verify(callback, never()).onMessage( + any(JSPackagerWebSocketClient.WebSocketSender.class), + anyString(), + anyString()); } @Test @@ -84,7 +99,10 @@ public class JSPackagerWebSocketClientTest { final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback); client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"version\": 1, \"target\": \"targetValue\", \"action\": null}")); - verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString()); + verify(callback, never()).onMessage( + any(JSPackagerWebSocketClient.WebSocketSender.class), + anyString(), + anyString()); } @Test @@ -94,6 +112,9 @@ public class JSPackagerWebSocketClientTest { final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback); client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"version\": 2, \"target\": \"targetValue\", \"action\": \"actionValue\"}")); - verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString()); + verify(callback, never()).onMessage( + any(JSPackagerWebSocketClient.WebSocketSender.class), + anyString(), + anyString()); } }