diff --git a/Libraries/WebSocket/WebSocket.js b/Libraries/WebSocket/WebSocket.js index a328aa5b4..3ce40bc67 100644 --- a/Libraries/WebSocket/WebSocket.js +++ b/Libraries/WebSocket/WebSocket.js @@ -104,7 +104,7 @@ class WebSocket extends WebSocketBase { this.onclose && this.onclose(event); this.dispatchEvent(event); this._unregisterEvents(); - this._closeWebSocket(id); + this.close(); }), RCTDeviceEventEmitter.addListener('websocketFailed', ev => { if (ev.id !== id) { @@ -115,7 +115,7 @@ class WebSocket extends WebSocketBase { this.onerror && this.onerror(event); this.dispatchEvent(event); this._unregisterEvents(); - this.readyState === this.OPEN && this._closeWebSocket(id); + this.close(); }) ]; } diff --git a/Libraries/WebSocket/WebSocketBase.js b/Libraries/WebSocket/WebSocketBase.js index aa4777c91..45b3d6dec 100644 --- a/Libraries/WebSocket/WebSocketBase.js +++ b/Libraries/WebSocket/WebSocketBase.js @@ -44,6 +44,7 @@ class WebSocketBase extends EventTarget { protocols = []; } + this.readyState = WebSocketBase.CONNECTING; this.connectToSocketImpl(url); } @@ -57,6 +58,7 @@ class WebSocketBase extends EventTarget { this.cancelConnectionImpl(); } + this.readyState = WebSocketBase.CLOSING; this.closeConnectionImpl(); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java index 0e5ce7e9b..dfa41638c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java @@ -131,8 +131,13 @@ public class WebSocketModule extends ReactContextBaseJavaModule { public void close(int code, String reason, int id) { WebSocket client = mWebSocketConnections.get(id); if (client == null) { - // This is a programmer error - throw new RuntimeException("Cannot close WebSocket. Unknown WebSocket id " + id); + // WebSocket is already closed + // Don't do anything, mirror the behaviour on web + FLog.w( + ReactConstants.TAG, + "Cannot close WebSocket. Unknown WebSocket id " + id); + + return; } try { client.close(code, reason);