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
This commit is contained in:
Pieter De Baets
2022-11-02 10:24:56 -07:00
committed by Facebook GitHub Bot
parent 6a23b131e5
commit b5ea5a2c4d
@@ -58,10 +58,18 @@ public final class WebSocketModule extends NativeWebSocketModuleSpec {
mCookieHandler = new ForwardingCookieHandler(context);
}
private void sendEvent(String eventName, WritableMap params) {
ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn();
@Override
public void invalidate() {
for (WebSocket socket : mWebSocketConnections.values()) {
socket.close(1001 /* endpoint is going away */, null);
}
mWebSocketConnections.clear();
mContentHandlers.clear();
}
if (reactApplicationContext != null) {
private void sendEvent(String eventName, WritableMap params) {
ReactApplicationContext reactApplicationContext = getReactApplicationContext();
if (reactApplicationContext.hasActiveReactInstance()) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);