forked from kudos/koa-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (57 loc) · 1.71 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
60
61
62
63
64
65
'use strict';
const url = require('url'),
https = require('https'),
compose = require('koa-compose'),
co = require('co'),
ws = require('ws');
const WebSocketServer = ws.Server;
const debug = require('debug')('koa:websockets');
function KoaWebSocketServer (app) {
this.app = app;
this.middleware = [];
}
KoaWebSocketServer.prototype.listen = function (options) {
this.server = new WebSocketServer(options);
this.server.on('connection', this.onConnection.bind(this));
};
KoaWebSocketServer.prototype.onConnection = function(socket, request) {
debug('Connection received');
socket.on('error', function (err) {
debug('Error occurred:', err);
});
const fn = co.wrap(compose(this.middleware));
const context = this.app.createContext(request);
context.websocket = socket;
context.path = url.parse(request.url).pathname;
fn(context).catch(function(err) {
debug(err);
});
};
KoaWebSocketServer.prototype.use = function (fn) {
this.middleware.push(fn);
return this;
};
module.exports = function (app, wsOptions, httpsOptions) {
const oldListen = app.listen;
app.listen = function () {
debug('Attaching server...');
if (typeof httpsOptions === 'object') {
const httpsServer = https.createServer(httpsOptions, app.callback());
app.server = httpsServer.listen.apply(httpsServer, arguments);
} else {
app.server = oldListen.apply(app, arguments);
}
const options = { server: app.server};
if (wsOptions) {
for (var key in wsOptions) {
if (wsOptions.hasOwnProperty(key)) {
options[key] = wsOptions[key];
}
}
}
app.ws.listen(options);
return app.server;
};
app.ws = new KoaWebSocketServer(app);
return app;
};