diff --git a/Libraries/WebSocket/NativeWebSocketModule.js b/Libraries/WebSocket/NativeWebSocketModule.js new file mode 100644 index 00000000000000..8a0fa330ed285f --- /dev/null +++ b/Libraries/WebSocket/NativeWebSocketModule.js @@ -0,0 +1,33 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +'use strict'; + +import type {TurboModule} from 'RCTExport'; +import * as TurboModuleRegistry from 'TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +connect: ( + url: string, + protocols: ?Array, + options: ?{headers?: {origin?: string}}, + socketID: number, + ) => void; + +send: (message: string, socketID: number) => void; + +sendBinary: (base64String: string, socketID: number) => void; + +ping: (socketID: number) => void; + +close: (code: number, reason: string, socketID: number) => void; + + // Events + +addListener: (eventName: string) => void; + +removeListeners: (count: number) => void; +} + +export default TurboModuleRegistry.getEnforcing('WebSocketModule'); diff --git a/Libraries/WebSocket/WebSocket.js b/Libraries/WebSocket/WebSocket.js index 472fd833e63096..47d56c7eecd7d7 100644 --- a/Libraries/WebSocket/WebSocket.js +++ b/Libraries/WebSocket/WebSocket.js @@ -14,7 +14,6 @@ const Blob = require('../Blob/Blob'); const EventTarget = require('event-target-shim'); const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter'); const BlobManager = require('../Blob/BlobManager'); -const NativeModules = require('../BatchedBridge/NativeModules'); const Platform = require('../Utilities/Platform'); const WebSocketEvent = require('./WebSocketEvent'); @@ -22,7 +21,7 @@ const base64 = require('base64-js'); const binaryToBase64 = require('../Utilities/binaryToBase64'); const invariant = require('invariant'); -const {WebSocketModule} = NativeModules; +import NativeWebSocketModule from './NativeWebSocketModule'; import type EventSubscription from '../vendor/emitter/EventSubscription'; @@ -128,10 +127,10 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) { protocols = null; } - this._eventEmitter = new NativeEventEmitter(WebSocketModule); + this._eventEmitter = new NativeEventEmitter(NativeWebSocketModule); this._socketId = nextWebSocketId++; this._registerEvents(); - WebSocketModule.connect(url, protocols, {headers}, this._socketId); + NativeWebSocketModule.connect(url, protocols, {headers}, this._socketId); } get binaryType(): ?BinaryType { @@ -180,12 +179,12 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) { } if (typeof data === 'string') { - WebSocketModule.send(data, this._socketId); + NativeWebSocketModule.send(data, this._socketId); return; } if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) { - WebSocketModule.sendBinary(binaryToBase64(data), this._socketId); + NativeWebSocketModule.sendBinary(binaryToBase64(data), this._socketId); return; } @@ -197,14 +196,14 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) { throw new Error('INVALID_STATE_ERR'); } - WebSocketModule.ping(this._socketId); + NativeWebSocketModule.ping(this._socketId); } _close(code?: number, reason?: string): void { // 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); + NativeWebSocketModule.close(statusCode, closeReason, this._socketId); if (BlobManager.isAvailable && this._binaryType === 'blob') { BlobManager.removeWebSocketHandler(this._socketId); diff --git a/Libraries/WebSocket/WebSocketInterceptor.js b/Libraries/WebSocket/WebSocketInterceptor.js index 58cd3da3a3b7d7..6c371c1028c485 100644 --- a/Libraries/WebSocket/WebSocketInterceptor.js +++ b/Libraries/WebSocket/WebSocketInterceptor.js @@ -9,16 +9,16 @@ 'use strict'; -const RCTWebSocketModule = require('../BatchedBridge/NativeModules') - .WebSocketModule; const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter'); +import NativeWebSocketModule from './NativeWebSocketModule'; + const base64 = require('base64-js'); -const originalRCTWebSocketConnect = RCTWebSocketModule.connect; -const originalRCTWebSocketSend = RCTWebSocketModule.send; -const originalRCTWebSocketSendBinary = RCTWebSocketModule.sendBinary; -const originalRCTWebSocketClose = RCTWebSocketModule.close; +const originalRCTWebSocketConnect = NativeWebSocketModule.connect; +const originalRCTWebSocketSend = NativeWebSocketModule.send; +const originalRCTWebSocketSendBinary = NativeWebSocketModule.sendBinary; +const originalRCTWebSocketClose = NativeWebSocketModule.close; let eventEmitter: NativeEventEmitter; let subscriptions: Array; @@ -135,13 +135,18 @@ const WebSocketInterceptor = { if (isInterceptorEnabled) { return; } - eventEmitter = new NativeEventEmitter(RCTWebSocketModule); + eventEmitter = new NativeEventEmitter(NativeWebSocketModule); WebSocketInterceptor._registerEvents(); // Override `connect` method for all RCTWebSocketModule requests // to intercept the request url, protocols, options and socketId, // then pass them through the `connectCallback`. - RCTWebSocketModule.connect = function(url, protocols, options, socketId) { + NativeWebSocketModule.connect = function( + url, + protocols, + options, + socketId, + ) { if (connectCallback) { connectCallback(url, protocols, options, socketId); } @@ -150,7 +155,7 @@ const WebSocketInterceptor = { // Override `send` method for all RCTWebSocketModule requests to intercept // the data sent, then pass them through the `sendCallback`. - RCTWebSocketModule.send = function(data, socketId) { + NativeWebSocketModule.send = function(data, socketId) { if (sendCallback) { sendCallback(data, socketId); } @@ -159,7 +164,7 @@ const WebSocketInterceptor = { // Override `sendBinary` method for all RCTWebSocketModule requests to // intercept the data sent, then pass them through the `sendCallback`. - RCTWebSocketModule.sendBinary = function(data, socketId) { + NativeWebSocketModule.sendBinary = function(data, socketId) { if (sendCallback) { sendCallback(WebSocketInterceptor._arrayBufferToString(data), socketId); } @@ -168,7 +173,7 @@ const WebSocketInterceptor = { // Override `close` method for all RCTWebSocketModule requests to intercept // the close information, then pass them through the `closeCallback`. - RCTWebSocketModule.close = function() { + NativeWebSocketModule.close = function() { if (closeCallback) { if (arguments.length === 3) { closeCallback(arguments[0], arguments[1], arguments[2]); @@ -203,10 +208,10 @@ const WebSocketInterceptor = { return; } isInterceptorEnabled = false; - RCTWebSocketModule.send = originalRCTWebSocketSend; - RCTWebSocketModule.sendBinary = originalRCTWebSocketSendBinary; - RCTWebSocketModule.close = originalRCTWebSocketClose; - RCTWebSocketModule.connect = originalRCTWebSocketConnect; + NativeWebSocketModule.send = originalRCTWebSocketSend; + NativeWebSocketModule.sendBinary = originalRCTWebSocketSendBinary; + NativeWebSocketModule.close = originalRCTWebSocketClose; + NativeWebSocketModule.connect = originalRCTWebSocketConnect; connectCallback = null; closeCallback = null;