2015-05-14 16:28:09 +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 WebSocketBase
|
2016-02-16 11:28:05 +00:00
|
|
|
* @flow
|
2015-05-14 16:28:09 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-10-02 00:38:40 +00:00
|
|
|
var EventTarget = require('event-target-shim');
|
|
|
|
|
2016-02-16 11:28:05 +00:00
|
|
|
const CONNECTING = 0;
|
|
|
|
const OPEN = 1;
|
|
|
|
const CLOSING = 2;
|
|
|
|
const CLOSED = 3;
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
/**
|
|
|
|
* Shared base for platform-specific WebSocket implementations.
|
|
|
|
*/
|
2015-10-02 00:38:40 +00:00
|
|
|
class WebSocketBase extends EventTarget {
|
2015-05-14 16:28:09 +00:00
|
|
|
CONNECTING: number;
|
|
|
|
OPEN: number;
|
|
|
|
CLOSING: number;
|
|
|
|
CLOSED: number;
|
|
|
|
|
|
|
|
onclose: ?Function;
|
|
|
|
onerror: ?Function;
|
|
|
|
onmessage: ?Function;
|
|
|
|
onopen: ?Function;
|
|
|
|
|
|
|
|
binaryType: ?string;
|
|
|
|
bufferedAmount: number;
|
|
|
|
extension: ?string;
|
|
|
|
protocol: ?string;
|
|
|
|
readyState: number;
|
|
|
|
url: ?string;
|
|
|
|
|
2016-01-20 19:00:21 +00:00
|
|
|
constructor(url: string, protocols: ?string | ?Array<string>, options: ?{origin?: string}) {
|
2015-10-02 00:38:40 +00:00
|
|
|
super();
|
2016-02-16 11:28:05 +00:00
|
|
|
this.CONNECTING = CONNECTING;
|
|
|
|
this.OPEN = OPEN;
|
|
|
|
this.CLOSING = CLOSING;
|
|
|
|
this.CLOSED = CLOSED;
|
2015-05-14 16:28:09 +00:00
|
|
|
|
2016-01-20 19:00:21 +00:00
|
|
|
if (typeof protocols === 'string') {
|
|
|
|
protocols = [protocols];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(protocols)) {
|
|
|
|
protocols = null;
|
2015-05-14 16:28:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 19:23:29 +00:00
|
|
|
this.readyState = this.CONNECTING;
|
2016-01-20 19:00:21 +00:00
|
|
|
this.connectToSocketImpl(url, protocols, options);
|
2015-05-14 16:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
2015-11-06 19:23:29 +00:00
|
|
|
if (this.readyState === this.CLOSING ||
|
|
|
|
this.readyState === this.CLOSED) {
|
2015-05-14 16:28:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:23:29 +00:00
|
|
|
if (this.readyState === this.CONNECTING) {
|
2015-05-14 16:28:09 +00:00
|
|
|
this.cancelConnectionImpl();
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:23:29 +00:00
|
|
|
this.readyState = this.CLOSING;
|
2015-05-14 16:28:09 +00:00
|
|
|
this.closeConnectionImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
send(data: any): void {
|
2015-11-06 19:23:29 +00:00
|
|
|
if (this.readyState === this.CONNECTING) {
|
2015-05-14 16:28:09 +00:00
|
|
|
throw new Error('INVALID_STATE_ERR');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
this.sendStringImpl(data);
|
|
|
|
} else if (data instanceof ArrayBuffer) {
|
|
|
|
this.sendArrayBufferImpl(data);
|
|
|
|
} else {
|
|
|
|
throw new Error('Not supported data type');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closeConnectionImpl(): void {
|
|
|
|
throw new Error('Subclass must define closeConnectionImpl method');
|
|
|
|
}
|
|
|
|
|
2016-02-16 11:28:05 +00:00
|
|
|
connectToSocketImpl(url: string, protocols: ?Array<string>, options: ?{origin?: string}): void {
|
2015-05-14 16:28:09 +00:00
|
|
|
throw new Error('Subclass must define connectToSocketImpl method');
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelConnectionImpl(): void {
|
|
|
|
throw new Error('Subclass must define cancelConnectionImpl method');
|
|
|
|
}
|
|
|
|
|
2016-02-16 11:28:05 +00:00
|
|
|
sendStringImpl(message: string): void {
|
2015-05-14 16:28:09 +00:00
|
|
|
throw new Error('Subclass must define sendStringImpl method');
|
|
|
|
}
|
|
|
|
|
|
|
|
sendArrayBufferImpl(): void {
|
|
|
|
throw new Error('Subclass must define sendArrayBufferImpl method');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 11:28:05 +00:00
|
|
|
WebSocketBase.CONNECTING = CONNECTING;
|
|
|
|
WebSocketBase.OPEN = OPEN;
|
|
|
|
WebSocketBase.CLOSING = CLOSING;
|
|
|
|
WebSocketBase.CLOSED = CLOSED;
|
2015-11-06 19:23:29 +00:00
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
module.exports = WebSocketBase;
|