2015-10-07 15:28:34 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule WebSocket
|
2016-02-16 11:28:05 +00:00
|
|
|
* @flow
|
2015-10-07 15:28:34 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-12 15:29:39 +00:00
|
|
|
const NativeEventEmitter = require('NativeEventEmitter');
|
2016-04-18 22:42:42 +00:00
|
|
|
const Platform = require('Platform');
|
2016-05-12 15:29:39 +00:00
|
|
|
const RCTWebSocketModule = require('NativeModules').WebSocketModule;
|
2016-04-18 22:42:42 +00:00
|
|
|
const WebSocketEvent = require('WebSocketEvent');
|
2017-01-21 02:40:28 +00:00
|
|
|
const binaryToBase64 = require('binaryToBase64');
|
2015-10-07 15:28:34 +00:00
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
const EventTarget = require('event-target-shim');
|
|
|
|
const base64 = require('base64-js');
|
2015-10-07 15:28:34 +00:00
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
import type EventSubscription from 'EventSubscription';
|
2015-12-22 17:22:01 +00:00
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
const CONNECTING = 0;
|
|
|
|
const OPEN = 1;
|
|
|
|
const CLOSING = 2;
|
|
|
|
const CLOSED = 3;
|
|
|
|
|
|
|
|
const CLOSE_NORMAL = 1000;
|
|
|
|
|
|
|
|
const WEBSOCKET_EVENTS = [
|
|
|
|
'close',
|
|
|
|
'error',
|
|
|
|
'message',
|
|
|
|
'open',
|
|
|
|
];
|
|
|
|
|
|
|
|
let nextWebSocketId = 0;
|
2015-10-07 15:28:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Browser-compatible WebSockets implementation.
|
|
|
|
*
|
|
|
|
* See https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
|
2016-01-20 19:00:21 +00:00
|
|
|
* See https://github.com/websockets/ws
|
2015-10-07 15:28:34 +00:00
|
|
|
*/
|
2016-04-27 19:25:38 +00:00
|
|
|
class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
|
2016-04-18 22:42:42 +00:00
|
|
|
static CONNECTING = CONNECTING;
|
|
|
|
static OPEN = OPEN;
|
|
|
|
static CLOSING = CLOSING;
|
|
|
|
static CLOSED = CLOSED;
|
2015-10-07 15:28:34 +00:00
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
CONNECTING: number = CONNECTING;
|
|
|
|
OPEN: number = OPEN;
|
|
|
|
CLOSING: number = CLOSING;
|
|
|
|
CLOSED: number = CLOSED;
|
2015-10-07 15:28:34 +00:00
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
_socketId: number;
|
2016-05-12 15:29:39 +00:00
|
|
|
_eventEmitter: NativeEventEmitter;
|
2016-04-18 22:42:42 +00:00
|
|
|
_subscriptions: Array<EventSubscription>;
|
|
|
|
|
|
|
|
onclose: ?Function;
|
|
|
|
onerror: ?Function;
|
|
|
|
onmessage: ?Function;
|
|
|
|
onopen: ?Function;
|
|
|
|
|
|
|
|
binaryType: ?string;
|
|
|
|
bufferedAmount: number;
|
|
|
|
extension: ?string;
|
|
|
|
protocol: ?string;
|
|
|
|
readyState: number = CONNECTING;
|
|
|
|
url: ?string;
|
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
// This module depends on the native `RCTWebSocketModule` module. If you don't include it,
|
|
|
|
// `WebSocket.isAvailable` will return `false`, and WebSocket constructor will throw an error
|
|
|
|
static isAvailable: boolean = !!RCTWebSocketModule;
|
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
constructor(url: string, protocols: ?string | ?Array<string>, options: ?{origin?: string}) {
|
|
|
|
super();
|
|
|
|
if (typeof protocols === 'string') {
|
|
|
|
protocols = [protocols];
|
|
|
|
}
|
2015-10-07 15:28:34 +00:00
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
if (!Array.isArray(protocols)) {
|
|
|
|
protocols = null;
|
|
|
|
}
|
2015-10-07 15:28:34 +00:00
|
|
|
|
2017-06-01 15:20:57 +00:00
|
|
|
if (!WebSocket.isAvailable) {
|
|
|
|
throw new Error('Cannot initialize WebSocket module. ' +
|
|
|
|
'Native module RCTWebSocketModule is missing.');
|
|
|
|
}
|
|
|
|
|
2016-05-12 15:29:39 +00:00
|
|
|
this._eventEmitter = new NativeEventEmitter(RCTWebSocketModule);
|
2016-04-18 22:42:42 +00:00
|
|
|
this._socketId = nextWebSocketId++;
|
|
|
|
this._registerEvents();
|
2017-02-09 21:17:35 +00:00
|
|
|
RCTWebSocketModule.connect(url, protocols, options, this._socketId);
|
2015-10-07 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
close(code?: number, reason?: string): void {
|
|
|
|
if (this.readyState === this.CLOSING ||
|
|
|
|
this.readyState === this.CLOSED) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-07 15:28:34 +00:00
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
this.readyState = this.CLOSING;
|
|
|
|
this._close(code, reason);
|
2015-10-07 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 02:40:28 +00:00
|
|
|
send(data: string | ArrayBuffer | $ArrayBufferView): void {
|
2016-04-18 22:42:42 +00:00
|
|
|
if (this.readyState === this.CONNECTING) {
|
|
|
|
throw new Error('INVALID_STATE_ERR');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
RCTWebSocketModule.send(data, this._socketId);
|
2016-04-20 15:52:22 +00:00
|
|
|
return;
|
2016-04-18 22:42:42 +00:00
|
|
|
}
|
2016-04-20 15:52:22 +00:00
|
|
|
|
2017-01-21 02:40:28 +00:00
|
|
|
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
|
|
RCTWebSocketModule.sendBinary(binaryToBase64(data), this._socketId);
|
2016-09-08 14:28:31 +00:00
|
|
|
return;
|
2016-04-20 15:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Unsupported data type');
|
2015-10-07 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 12:52:24 +00:00
|
|
|
ping(): void {
|
|
|
|
if (this.readyState === this.CONNECTING) {
|
|
|
|
throw new Error('INVALID_STATE_ERR');
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTWebSocketModule.ping(this._socketId);
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
_close(code?: number, reason?: string): void {
|
2015-10-07 15:28:34 +00:00
|
|
|
if (Platform.OS === 'android') {
|
2016-04-18 22:42:42 +00:00
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
|
2016-05-12 15:29:39 +00:00
|
|
|
const statusCode = typeof code === 'number' ? code : CLOSE_NORMAL;
|
|
|
|
const closeReason = typeof reason === 'string' ? reason : '';
|
2016-04-18 22:42:42 +00:00
|
|
|
RCTWebSocketModule.close(statusCode, closeReason, this._socketId);
|
2015-10-07 15:28:34 +00:00
|
|
|
} else {
|
2016-04-18 22:42:42 +00:00
|
|
|
RCTWebSocketModule.close(this._socketId);
|
2015-10-07 15:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_unregisterEvents(): void {
|
2016-04-18 22:42:42 +00:00
|
|
|
this._subscriptions.forEach(e => e.remove());
|
|
|
|
this._subscriptions = [];
|
2015-10-07 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 22:42:42 +00:00
|
|
|
_registerEvents(): void {
|
|
|
|
this._subscriptions = [
|
2016-05-12 15:29:39 +00:00
|
|
|
this._eventEmitter.addListener('websocketMessage', ev => {
|
2016-04-18 22:42:42 +00:00
|
|
|
if (ev.id !== this._socketId) {
|
2015-10-07 15:28:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-12 15:29:39 +00:00
|
|
|
this.dispatchEvent(new WebSocketEvent('message', {
|
2015-12-22 17:22:01 +00:00
|
|
|
data: (ev.type === 'binary') ? base64.toByteArray(ev.data).buffer : ev.data
|
2016-05-12 15:29:39 +00:00
|
|
|
}));
|
2015-10-07 15:28:34 +00:00
|
|
|
}),
|
2016-05-12 15:29:39 +00:00
|
|
|
this._eventEmitter.addListener('websocketOpen', ev => {
|
2016-04-18 22:42:42 +00:00
|
|
|
if (ev.id !== this._socketId) {
|
2015-10-07 15:28:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.readyState = this.OPEN;
|
2016-05-12 15:29:39 +00:00
|
|
|
this.dispatchEvent(new WebSocketEvent('open'));
|
2015-10-07 15:28:34 +00:00
|
|
|
}),
|
2016-05-12 15:29:39 +00:00
|
|
|
this._eventEmitter.addListener('websocketClosed', ev => {
|
2016-04-18 22:42:42 +00:00
|
|
|
if (ev.id !== this._socketId) {
|
2015-10-07 15:28:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.readyState = this.CLOSED;
|
2016-05-12 15:29:39 +00:00
|
|
|
this.dispatchEvent(new WebSocketEvent('close', {
|
|
|
|
code: ev.code,
|
|
|
|
reason: ev.reason,
|
|
|
|
}));
|
2015-10-07 15:28:34 +00:00
|
|
|
this._unregisterEvents();
|
2015-10-27 15:33:38 +00:00
|
|
|
this.close();
|
2015-10-07 15:28:34 +00:00
|
|
|
}),
|
2016-05-12 15:29:39 +00:00
|
|
|
this._eventEmitter.addListener('websocketFailed', ev => {
|
2016-04-18 22:42:42 +00:00
|
|
|
if (ev.id !== this._socketId) {
|
2015-10-07 15:28:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-09 00:01:10 +00:00
|
|
|
this.readyState = this.CLOSED;
|
2016-05-12 15:29:39 +00:00
|
|
|
this.dispatchEvent(new WebSocketEvent('error', {
|
|
|
|
message: ev.message,
|
|
|
|
}));
|
|
|
|
this.dispatchEvent(new WebSocketEvent('close', {
|
|
|
|
message: ev.message,
|
|
|
|
}));
|
2015-10-07 15:28:34 +00:00
|
|
|
this._unregisterEvents();
|
2015-10-27 15:33:38 +00:00
|
|
|
this.close();
|
2015-10-07 15:28:34 +00:00
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = WebSocket;
|