Initial commit
[yaffs-website] / node_modules / faye-websocket / lib / faye / websocket.js
1 // API references:
2 //
3 // * http://dev.w3.org/html5/websockets/
4 // * http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-eventtarget
5 // * http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-event
6
7 var util   = require('util'),
8     driver = require('websocket-driver'),
9     API    = require('./websocket/api');
10
11 var WebSocket = function(request, socket, body, protocols, options) {
12   options = options || {};
13
14   this._stream = socket;
15   this._driver = driver.http(request, {maxLength: options.maxLength, protocols: protocols});
16
17   var self = this;
18   if (!this._stream || !this._stream.writable) return;
19   if (!this._stream.readable) return this._stream.end();
20
21   var catchup = function() { self._stream.removeListener('data', catchup) };
22   this._stream.on('data', catchup);
23
24   API.call(this, options);
25
26   process.nextTick(function() {
27     self._driver.start();
28     self._driver.io.write(body);
29   });
30 };
31 util.inherits(WebSocket, API);
32
33 WebSocket.isWebSocket = function(request) {
34   return driver.isWebSocket(request);
35 };
36
37 WebSocket.validateOptions = function(options, validKeys) {
38   driver.validateOptions(options, validKeys);
39 };
40
41 WebSocket.WebSocket   = WebSocket;
42 WebSocket.Client      = require('./websocket/client');
43 WebSocket.EventSource = require('./eventsource');
44
45 module.exports        = WebSocket;