mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add spec for WebSocketModule (#24893)
Summary: Part of #24875 ## Changelog [General] [Added] - Add TurboModule spec for WebSocketModule Pull Request resolved: https://github.com/facebook/react-native/pull/24893 Reviewed By: RSNara Differential Revision: D15551329 Pulled By: fkgozali fbshipit-source-id: 59a921c50cc162528b2181fdd4cb1e41e3f1f6eb
This commit is contained in:
committed by
Facebook Github Bot
parent
00c0fe7d5b
commit
5705ea1752
@@ -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 strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type {TurboModule} from '../TurboModule/RCTExport';
|
||||
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
+connect: (
|
||||
url: string,
|
||||
protocols: ?Array<string>,
|
||||
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;
|
||||
|
||||
// RCTEventEmitter
|
||||
+addListener: (eventName: string) => void;
|
||||
+removeListeners: (count: number) => void;
|
||||
}
|
||||
|
||||
export default TurboModuleRegistry.getEnforcing<Spec>('WebSocketModule');
|
||||
@@ -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);
|
||||
|
||||
@@ -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<EventSubscription>;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user