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