UNPKG

8.59 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 * @flow
9 */
10
11'use strict';
12
13const Blob = require('Blob');
14const EventTarget = require('event-target-shim');
15const NativeEventEmitter = require('NativeEventEmitter');
16const BlobManager = require('BlobManager');
17const NativeModules = require('NativeModules');
18const Platform = require('Platform');
19const WebSocketEvent = require('WebSocketEvent');
20
21/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
22 * found when Flow v0.54 was deployed. To see the error delete this comment and
23 * run Flow. */
24const base64 = require('base64-js');
25const binaryToBase64 = require('binaryToBase64');
26const invariant = require('invariant');
27
28const {WebSocketModule} = NativeModules;
29
30import type EventSubscription from 'EventSubscription';
31
32type ArrayBufferView =
33 | Int8Array
34 | Uint8Array
35 | Uint8ClampedArray
36 | Int16Array
37 | Uint16Array
38 | Int32Array
39 | Uint32Array
40 | Float32Array
41 | Float64Array
42 | DataView;
43
44type BinaryType = 'blob' | 'arraybuffer';
45
46const CONNECTING = 0;
47const OPEN = 1;
48const CLOSING = 2;
49const CLOSED = 3;
50
51const CLOSE_NORMAL = 1000;
52
53const WEBSOCKET_EVENTS = ['close', 'error', 'message', 'open'];
54
55let nextWebSocketId = 0;
56
57/**
58 * Browser-compatible WebSockets implementation.
59 *
60 * See https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
61 * See https://github.com/websockets/ws
62 */
63class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
64 static CONNECTING = CONNECTING;
65 static OPEN = OPEN;
66 static CLOSING = CLOSING;
67 static CLOSED = CLOSED;
68
69 CONNECTING: number = CONNECTING;
70 OPEN: number = OPEN;
71 CLOSING: number = CLOSING;
72 CLOSED: number = CLOSED;
73
74 _socketId: number;
75 _eventEmitter: NativeEventEmitter;
76 _subscriptions: Array<EventSubscription>;
77 _binaryType: ?BinaryType;
78
79 onclose: ?Function;
80 onerror: ?Function;
81 onmessage: ?Function;
82 onopen: ?Function;
83
84 bufferedAmount: number;
85 extension: ?string;
86 protocol: ?string;
87 readyState: number = CONNECTING;
88 url: ?string;
89
90 // This module depends on the native `WebSocketModule` module. If you don't include it,
91 // `WebSocket.isAvailable` will return `false`, and WebSocket constructor will throw an error
92 static isAvailable: boolean = !!WebSocketModule;
93
94 constructor(
95 url: string,
96 protocols: ?string | ?Array<string>,
97 options: ?{headers?: {origin?: string}},
98 ) {
99 super();
100 if (typeof protocols === 'string') {
101 protocols = [protocols];
102 }
103
104 const {headers = {}, ...unrecognized} = options || {};
105
106 // Preserve deprecated backwards compatibility for the 'origin' option
107 /* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an
108 * error found when Flow v0.68 was deployed. To see the error delete this
109 * comment and run Flow. */
110 if (unrecognized && typeof unrecognized.origin === 'string') {
111 console.warn(
112 'Specifying `origin` as a WebSocket connection option is deprecated. Include it under `headers` instead.',
113 );
114 /* $FlowFixMe(>=0.54.0 site=react_native_fb,react_native_oss) This
115 * comment suppresses an error found when Flow v0.54 was deployed. To see
116 * the error delete this comment and run Flow. */
117 headers.origin = unrecognized.origin;
118 /* $FlowFixMe(>=0.54.0 site=react_native_fb,react_native_oss) This
119 * comment suppresses an error found when Flow v0.54 was deployed. To see
120 * the error delete this comment and run Flow. */
121 delete unrecognized.origin;
122 }
123
124 // Warn about and discard anything else
125 if (Object.keys(unrecognized).length > 0) {
126 console.warn(
127 'Unrecognized WebSocket connection option(s) `' +
128 Object.keys(unrecognized).join('`, `') +
129 '`. ' +
130 'Did you mean to put these under `headers`?',
131 );
132 }
133
134 if (!Array.isArray(protocols)) {
135 protocols = null;
136 }
137
138 if (!WebSocket.isAvailable) {
139 throw new Error(
140 'Cannot initialize WebSocket module. ' +
141 'Native module WebSocketModule is missing.',
142 );
143 }
144
145 this._eventEmitter = new NativeEventEmitter(WebSocketModule);
146 this._socketId = nextWebSocketId++;
147 this._registerEvents();
148 WebSocketModule.connect(
149 url,
150 protocols,
151 {headers},
152 this._socketId,
153 );
154 }
155
156 get binaryType(): ?BinaryType {
157 return this._binaryType;
158 }
159
160 set binaryType(binaryType: BinaryType): void {
161 if (binaryType !== 'blob' && binaryType !== 'arraybuffer') {
162 throw new Error("binaryType must be either 'blob' or 'arraybuffer'");
163 }
164 if (this._binaryType === 'blob' || binaryType === 'blob') {
165 invariant(
166 BlobManager.isAvailable,
167 'Native module BlobModule is required for blob support',
168 );
169 if (binaryType === 'blob') {
170 BlobManager.addWebSocketHandler(this._socketId);
171 } else {
172 BlobManager.removeWebSocketHandler(this._socketId);
173 }
174 }
175 this._binaryType = binaryType;
176 }
177
178 close(code?: number, reason?: string): void {
179 if (this.readyState === this.CLOSING || this.readyState === this.CLOSED) {
180 return;
181 }
182
183 this.readyState = this.CLOSING;
184 this._close(code, reason);
185 }
186
187 send(data: string | ArrayBuffer | ArrayBufferView | Blob): void {
188 if (this.readyState === this.CONNECTING) {
189 throw new Error('INVALID_STATE_ERR');
190 }
191
192 if (data instanceof Blob) {
193 invariant(
194 BlobManager.isAvailable,
195 'Native module BlobModule is required for blob support',
196 );
197 BlobManager.sendOverSocket(data, this._socketId);
198 return;
199 }
200
201 if (typeof data === 'string') {
202 WebSocketModule.send(data, this._socketId);
203 return;
204 }
205
206 if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
207 WebSocketModule.sendBinary(binaryToBase64(data), this._socketId);
208 return;
209 }
210
211 throw new Error('Unsupported data type');
212 }
213
214 ping(): void {
215 if (this.readyState === this.CONNECTING) {
216 throw new Error('INVALID_STATE_ERR');
217 }
218
219 WebSocketModule.ping(this._socketId);
220 }
221
222 _close(code?: number, reason?: string): void {
223 if (Platform.OS === 'android') {
224 // See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
225 const statusCode = typeof code === 'number' ? code : CLOSE_NORMAL;
226 const closeReason = typeof reason === 'string' ? reason : '';
227 WebSocketModule.close(statusCode, closeReason, this._socketId);
228 } else {
229 WebSocketModule.close(this._socketId);
230 }
231
232 if (BlobManager.isAvailable && this._binaryType === 'blob') {
233 BlobManager.removeWebSocketHandler(this._socketId);
234 }
235 }
236
237 _unregisterEvents(): void {
238 this._subscriptions.forEach(e => e.remove());
239 this._subscriptions = [];
240 }
241
242 _registerEvents(): void {
243 this._subscriptions = [
244 this._eventEmitter.addListener('websocketMessage', ev => {
245 if (ev.id !== this._socketId) {
246 return;
247 }
248 let data = ev.data;
249 switch (ev.type) {
250 case 'binary':
251 data = base64.toByteArray(ev.data).buffer;
252 break;
253 case 'blob':
254 data = BlobManager.createFromOptions(ev.data);
255 break;
256 }
257 this.dispatchEvent(new WebSocketEvent('message', {data}));
258 }),
259 this._eventEmitter.addListener('websocketOpen', ev => {
260 if (ev.id !== this._socketId) {
261 return;
262 }
263 this.readyState = this.OPEN;
264 this.dispatchEvent(new WebSocketEvent('open'));
265 }),
266 this._eventEmitter.addListener('websocketClosed', ev => {
267 if (ev.id !== this._socketId) {
268 return;
269 }
270 this.readyState = this.CLOSED;
271 this.dispatchEvent(
272 new WebSocketEvent('close', {
273 code: ev.code,
274 reason: ev.reason,
275 }),
276 );
277 this._unregisterEvents();
278 this.close();
279 }),
280 this._eventEmitter.addListener('websocketFailed', ev => {
281 if (ev.id !== this._socketId) {
282 return;
283 }
284 this.readyState = this.CLOSED;
285 this.dispatchEvent(
286 new WebSocketEvent('error', {
287 message: ev.message,
288 }),
289 );
290 this.dispatchEvent(
291 new WebSocketEvent('close', {
292 message: ev.message,
293 }),
294 );
295 this._unregisterEvents();
296 this.close();
297 }),
298 ];
299 }
300}
301
302module.exports = WebSocket;