Skip to content

Commit c45bb3e

Browse files
satya164facebook-github-bot-0
authored andcommitted
Don't throw runtime exception if WebSocket is already closed
Summary: Refer facebook#3364 Closes facebook#3706 Reviewed By: svcscm Differential Revision: D2585455 Pulled By: mkonicek fb-gh-sync-id: fecd5e46c59a79a109aad97a49c9ea016e82d669
1 parent b28ff04 commit c45bb3e

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Libraries/WebSocket/WebSocket.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class WebSocket extends WebSocketBase {
104104
this.onclose && this.onclose(event);
105105
this.dispatchEvent(event);
106106
this._unregisterEvents();
107-
this._closeWebSocket(id);
107+
this.close();
108108
}),
109109
RCTDeviceEventEmitter.addListener('websocketFailed', ev => {
110110
if (ev.id !== id) {
@@ -115,7 +115,7 @@ class WebSocket extends WebSocketBase {
115115
this.onerror && this.onerror(event);
116116
this.dispatchEvent(event);
117117
this._unregisterEvents();
118-
this.readyState === this.OPEN && this._closeWebSocket(id);
118+
this.close();
119119
})
120120
];
121121
}

Libraries/WebSocket/WebSocketBase.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class WebSocketBase extends EventTarget {
4444
protocols = [];
4545
}
4646

47+
this.readyState = WebSocketBase.CONNECTING;
4748
this.connectToSocketImpl(url);
4849
}
4950

@@ -57,6 +58,7 @@ class WebSocketBase extends EventTarget {
5758
this.cancelConnectionImpl();
5859
}
5960

61+
this.readyState = WebSocketBase.CLOSING;
6062
this.closeConnectionImpl();
6163
}
6264

ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,13 @@ public void onMessage(BufferedSource bufferedSource, WebSocket.PayloadType paylo
131131
public void close(int code, String reason, int id) {
132132
WebSocket client = mWebSocketConnections.get(id);
133133
if (client == null) {
134-
// This is a programmer error
135-
throw new RuntimeException("Cannot close WebSocket. Unknown WebSocket id " + id);
134+
// WebSocket is already closed
135+
// Don't do anything, mirror the behaviour on web
136+
FLog.w(
137+
ReactConstants.TAG,
138+
"Cannot close WebSocket. Unknown WebSocket id " + id);
139+
140+
return;
136141
}
137142
try {
138143
client.close(code, reason);

0 commit comments

Comments
 (0)