From 0ac2171c549b389228c4a37ae645eb0d9813b82d Mon Sep 17 00:00:00 2001 From: Jean Regisser Date: Mon, 20 May 2019 01:06:33 -0700 Subject: [PATCH] Fixed code and reason arguments ignored when closing a WebSocket on iOS (#24950) Summary: While working on https://github.com/facebook/react-native/pull/24893 I noticed the `WebSocket` module implementation on iOS was ignoring the code and reason arguments for the `close` method. The Android implementation already handled those arguments properly. So this PR brings iOS implementation on par with Android for the `WebSocket` module. ## Changelog [iOS] [Fixed] - Fixed `code` and `reason` arguments ignored on iOS when calling `WebSocket.close` Pull Request resolved: https://github.com/facebook/react-native/pull/24950 Differential Revision: D15411922 Pulled By: cpojer fbshipit-source-id: f8a25598bd9c727313e24fea3801d5884d0723e4 --- Libraries/WebSocket/RCTWebSocketModule.m | 4 ++-- Libraries/WebSocket/WebSocket.js | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Libraries/WebSocket/RCTWebSocketModule.m b/Libraries/WebSocket/RCTWebSocketModule.m index af8e77065c055e..0150cd59582f5c 100644 --- a/Libraries/WebSocket/RCTWebSocketModule.m +++ b/Libraries/WebSocket/RCTWebSocketModule.m @@ -112,9 +112,9 @@ - (void)sendData:(NSData *)data forSocketID:(nonnull NSNumber *)socketID [_sockets[socketID] sendPing:NULL]; } -RCT_EXPORT_METHOD(close:(nonnull NSNumber *)socketID) +RCT_EXPORT_METHOD(close:(NSInteger)code reason:(NSString *)reason socketID:(nonnull NSNumber *)socketID) { - [_sockets[socketID] close]; + [_sockets[socketID] closeWithCode:code reason:reason]; [_sockets removeObjectForKey:socketID]; } diff --git a/Libraries/WebSocket/WebSocket.js b/Libraries/WebSocket/WebSocket.js index 0bd3d8632fd003..472fd833e63096 100644 --- a/Libraries/WebSocket/WebSocket.js +++ b/Libraries/WebSocket/WebSocket.js @@ -201,14 +201,10 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) { } _close(code?: number, reason?: string): void { - if (Platform.OS === 'android') { - // See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent - const statusCode = typeof code === 'number' ? code : CLOSE_NORMAL; - const closeReason = typeof reason === 'string' ? reason : ''; - WebSocketModule.close(statusCode, closeReason, this._socketId); - } else { - WebSocketModule.close(this._socketId); - } + // See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent + const statusCode = typeof code === 'number' ? code : CLOSE_NORMAL; + const closeReason = typeof reason === 'string' ? reason : ''; + WebSocketModule.close(statusCode, closeReason, this._socketId); if (BlobManager.isAvailable && this._binaryType === 'blob') { BlobManager.removeWebSocketHandler(this._socketId);