1 var ws
= require('socket.io'),
2 events
= require('events'),
3 http
= require('http'),
4 https
= require('https'),
5 util
= require('util'),
8 _
= require('underscore'),
9 Client
= require('./client.js').Client
;
10 HTTPHandler
= require('./http-handler.js').HTTPHandler
;
12 var WebListener = function (config
, transports
) {
18 events
.EventEmitter
.call(this);
20 http_handler
= new HTTPHandler(config
);
24 key
: fs
.readFileSync(__dirname
+ '/' + config
.ssl_key
),
25 cert
: fs
.readFileSync(__dirname
+ '/' + config
.ssl_cert
)
27 // Do we have an intermediate certificate?
28 if (typeof config
.ssl_ca
!== 'undefined') {
29 opts
.ca
= fs
.readFileSync(__dirname
+ '/' + config
.ssl_ca
);
31 hs
= https
.createServer(opts
, function (request
, response
) {
32 http_handler
.handler(request
, response
);
35 this.ws
= ws
.listen(hs
, {secure
: true});
36 hs
.listen(config
.port
, config
.address
);
37 console
.log('Listening on ' + config
.address
+ ':' + config
.port
.toString() + ' with SSL');
39 // Start some plain-text server up
40 hs
= http
.createServer(function (request
, response
) {
41 http_handler
.handler(request
, response
);
43 this.ws
= ws
.listen(hs
, {secure
: false});
44 hs
.listen(config
.port
, config
.address
);
45 console
.log('Listening on ' + config
.address
+ ':' + config
.port
.toString() + ' without SSL');
48 this.ws
.set('log level', 1);
49 this.ws
.enable('browser client minification');
50 this.ws
.enable('browser client etag');
51 this.ws
.set('transports', transports
);
53 this.ws
.of('/kiwi').authorization(authorisation
).on('connection', function () {
54 connection
.apply(that
, arguments
);
56 this.ws
.of('/kiwi').on('error', console
.log
);
58 util
.inherits(WebListener
, events
.EventEmitter
);
60 module
.exports
.WebListener
= WebListener
;
62 var authorisation = function (handshakeData
, callback
) {
63 dns
.reverse(handshakeData
.address
.address
, function (err
, domains
) {
64 handshakeData
.revdns
= (err
) ? handshakeData
.address
.address
: _
.first(domains
);
69 var connection = function (websocket
) {
70 //console.log(websocket);
71 this.emit('connection', new Client(websocket
));