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