-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (53 loc) · 1.58 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
var sockjs = require('sockjs');
exports = module.exports = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
var server = sockjs.createServer();
var handler = function (stream) {
var _didTimeout = stream._session.didTimeout
var _didClose = stream._session.didClose
stream._session.didTimeout = function () {
cleanup()
_didTimeout.apply(this, arguments)
}
stream._session.didClose = function () {
cleanup()
_didClose.apply(this, arguments)
}
cb(stream)
function cleanup() {
stream.emit("close")
if (stream.destroy) {
stream.destroy()
}
}
}
if (typeof cb === 'function') {
server.on('connection', handler);
}
server.install = function (httpServer, hopts) {
if (hopts && hopts.listen && !httpServer.listen) {
httpServer = arguments[1];
hopts = arguments[0];
}
if (typeof hopts === 'string') {
hopts = { prefix : hopts };
}
if (!hopts) hopts = {};
if (hopts.log === undefined) {
// spamming stdout by default is VERY infuriating,
// emit an event instead
hopts.log = function (severity, line) {
server.emit('log', severity, line);
};
}
server.installHandlers(httpServer, hopts);
return server;
};
return server;
};
for (var key in sockjs) {
exports[key] = sockjs[key];
}