1 | // API references:
|
---|
2 | //
|
---|
3 | // * https://html.spec.whatwg.org/multipage/comms.html#network
|
---|
4 | // * https://dom.spec.whatwg.org/#interface-eventtarget
|
---|
5 | // * https://dom.spec.whatwg.org/#interface-event
|
---|
6 |
|
---|
7 | 'use strict';
|
---|
8 |
|
---|
9 | var util = require('util'),
|
---|
10 | driver = require('websocket-driver'),
|
---|
11 | API = require('./websocket/api');
|
---|
12 |
|
---|
13 | var WebSocket = function(request, socket, body, protocols, options) {
|
---|
14 | options = options || {};
|
---|
15 |
|
---|
16 | this._stream = socket;
|
---|
17 | this._driver = driver.http(request, { maxLength: options.maxLength, protocols: protocols });
|
---|
18 |
|
---|
19 | var self = this;
|
---|
20 | if (!this._stream || !this._stream.writable) return;
|
---|
21 | if (!this._stream.readable) return this._stream.end();
|
---|
22 |
|
---|
23 | var catchup = function() { self._stream.removeListener('data', catchup) };
|
---|
24 | this._stream.on('data', catchup);
|
---|
25 |
|
---|
26 | API.call(this, options);
|
---|
27 |
|
---|
28 | process.nextTick(function() {
|
---|
29 | self._driver.start();
|
---|
30 | self._driver.io.write(body);
|
---|
31 | });
|
---|
32 | };
|
---|
33 | util.inherits(WebSocket, API);
|
---|
34 |
|
---|
35 | WebSocket.isWebSocket = function(request) {
|
---|
36 | return driver.isWebSocket(request);
|
---|
37 | };
|
---|
38 |
|
---|
39 | WebSocket.validateOptions = function(options, validKeys) {
|
---|
40 | driver.validateOptions(options, validKeys);
|
---|
41 | };
|
---|
42 |
|
---|
43 | WebSocket.WebSocket = WebSocket;
|
---|
44 | WebSocket.Client = require('./websocket/client');
|
---|
45 | WebSocket.EventSource = require('./eventsource');
|
---|
46 |
|
---|
47 | module.exports = WebSocket;
|
---|