mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-29 12:45:37 +08:00
Don't throw runtime exception if WebSocket is already closed
Summary: Refer #3364 Closes https://github.com/facebook/react-native/pull/3706 Reviewed By: svcscm Differential Revision: D2585455 Pulled By: mkonicek fb-gh-sync-id: fecd5e46c59a79a109aad97a49c9ea016e82d669
This commit is contained in:
committed by
facebook-github-bot-0
parent
b28ff0451d
commit
c45bb3e9c9
@@ -104,7 +104,7 @@ class WebSocket extends WebSocketBase {
|
|||||||
this.onclose && this.onclose(event);
|
this.onclose && this.onclose(event);
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
this._unregisterEvents();
|
this._unregisterEvents();
|
||||||
this._closeWebSocket(id);
|
this.close();
|
||||||
}),
|
}),
|
||||||
RCTDeviceEventEmitter.addListener('websocketFailed', ev => {
|
RCTDeviceEventEmitter.addListener('websocketFailed', ev => {
|
||||||
if (ev.id !== id) {
|
if (ev.id !== id) {
|
||||||
@@ -115,7 +115,7 @@ class WebSocket extends WebSocketBase {
|
|||||||
this.onerror && this.onerror(event);
|
this.onerror && this.onerror(event);
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
this._unregisterEvents();
|
this._unregisterEvents();
|
||||||
this.readyState === this.OPEN && this._closeWebSocket(id);
|
this.close();
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ class WebSocketBase extends EventTarget {
|
|||||||
protocols = [];
|
protocols = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.readyState = WebSocketBase.CONNECTING;
|
||||||
this.connectToSocketImpl(url);
|
this.connectToSocketImpl(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,6 +58,7 @@ class WebSocketBase extends EventTarget {
|
|||||||
this.cancelConnectionImpl();
|
this.cancelConnectionImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.readyState = WebSocketBase.CLOSING;
|
||||||
this.closeConnectionImpl();
|
this.closeConnectionImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -131,8 +131,13 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
|||||||
public void close(int code, String reason, int id) {
|
public void close(int code, String reason, int id) {
|
||||||
WebSocket client = mWebSocketConnections.get(id);
|
WebSocket client = mWebSocketConnections.get(id);
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
// This is a programmer error
|
// WebSocket is already closed
|
||||||
throw new RuntimeException("Cannot close WebSocket. Unknown WebSocket id " + id);
|
// Don't do anything, mirror the behaviour on web
|
||||||
|
FLog.w(
|
||||||
|
ReactConstants.TAG,
|
||||||
|
"Cannot close WebSocket. Unknown WebSocket id " + id);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
client.close(code, reason);
|
client.close(code, reason);
|
||||||
|
|||||||
Reference in New Issue
Block a user