Skip to content

Commit b5ea5a2

Browse files
javachefacebook-github-bot
authored andcommitted
Fix WebSocketModule not closing connections on reload
Summary: Saw in the logs an ever increasing number of warnings coming from WebSocketModule about requesting an instance that has already gone away. On module invalidation we should close all outstanding websockets, as they will no longer be able to send events to JS. Changelog: [Android][Fixed] On instance destroy, websockets are correctly closed Reviewed By: mdvacca Differential Revision: D40897255 fbshipit-source-id: 1578de8baa342479d14ee1070c3314d45c7fbd8d
1 parent 6a23b13 commit b5ea5a2

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,18 @@ public WebSocketModule(ReactApplicationContext context) {
5858
mCookieHandler = new ForwardingCookieHandler(context);
5959
}
6060

61-
private void sendEvent(String eventName, WritableMap params) {
62-
ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn();
61+
@Override
62+
public void invalidate() {
63+
for (WebSocket socket : mWebSocketConnections.values()) {
64+
socket.close(1001 /* endpoint is going away */, null);
65+
}
66+
mWebSocketConnections.clear();
67+
mContentHandlers.clear();
68+
}
6369

64-
if (reactApplicationContext != null) {
70+
private void sendEvent(String eventName, WritableMap params) {
71+
ReactApplicationContext reactApplicationContext = getReactApplicationContext();
72+
if (reactApplicationContext.hasActiveReactInstance()) {
6573
reactApplicationContext
6674
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
6775
.emit(eventName, params);

0 commit comments

Comments
 (0)