diff --git a/packages/react-native/React/CoreModules/RCTWebSocketModule.mm b/packages/react-native/React/CoreModules/RCTWebSocketModule.mm index e29d76a76a23..3d36c0eb2a1a 100644 --- a/packages/react-native/React/CoreModules/RCTWebSocketModule.mm +++ b/packages/react-native/React/CoreModules/RCTWebSocketModule.mm @@ -140,6 +140,9 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message NSString *type; NSNumber *socketID = [webSocket reactTag]; + if (!socketID) { + return; + } id contentHandler = _contentHandlers[socketID]; if (contentHandler) { message = [contentHandler processWebsocketMessage:message forSocketID:socketID withType:&type]; @@ -152,18 +155,25 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message } } - [self sendEventWithName:@"websocketMessage" body:@{@"data" : message, @"type" : type, @"id" : webSocket.reactTag}]; + [self sendEventWithName:@"websocketMessage" body:@{@"data" : message, @"type" : type, @"id" : socketID}]; } - (void)webSocketDidOpen:(SRWebSocket *)webSocket { + NSNumber *socketID = [webSocket reactTag]; + if (!socketID) { + return; + } [self sendEventWithName:@"websocketOpen" - body:@{@"id" : webSocket.reactTag, @"protocol" : webSocket.protocol ? webSocket.protocol : @""}]; + body:@{@"id" : socketID, @"protocol" : webSocket.protocol ? webSocket.protocol : @""}]; } - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { NSNumber *socketID = [webSocket reactTag]; + if (!socketID) { + return; + } _contentHandlers[socketID] = nil; _sockets[socketID] = nil; NSDictionary *body = @@ -177,6 +187,9 @@ - (void)webSocket:(SRWebSocket *)webSocket wasClean:(BOOL)wasClean { NSNumber *socketID = [webSocket reactTag]; + if (!socketID) { + return; + } _contentHandlers[socketID] = nil; _sockets[socketID] = nil; [self sendEventWithName:@"websocketClosed"