-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
88 lines (69 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const http = require('http');
const WebSocket = require('ws');
const server = http.createServer();
server.on('request', function request(req, res) {
const body = http.STATUS_CODES[426];
res.writeHead(426, {
Connection: 'close',
'Content-Length': body.length,
'Content-Type': 'text/plain'
});
res.end(body);
});
const { env } = process;
const highWaterMark = +env.HIGH_WATER_MARK || 16384;
const wss = new WebSocket.Server({
maxPayload: +env.MAX_MESSAGE_SIZE || 64 * 1024,
server
});
wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.message = 0;
ws.on('error', console.error);
ws.on('message', message);
ws.on('pong', heartbeat);
});
function message(data, binary) {
this.isAlive = true;
this.message++;
this.send(data, { binary }, (err) => {
/* istanbul ignore if */
if (err) {
return;
}
if (--this.message === 0 && this.isPaused) {
this.resume();
}
});
if (this.bufferedAmount >= highWaterMark && !this.isPaused) {
this.pause();
// This is used only for testing.
this.emit('pause');
}
}
function heartbeat() {
this.isAlive = true;
}
setInterval(function interval() {
for (const ws of wss.clients) {
if (ws.isAlive === false) {
ws.terminate();
continue;
}
ws.isAlive = false;
ws.ping();
}
}, +env.HEARTBEAT_INTERVAL || 30000).unref();
if (require.main === module) {
server.on('listening', function listening() {
const { address, family, port } = server.address();
console.log(
'Server listening on %s:%d',
family === 'IPv6' ? `[${address}]` : address,
port
);
});
server.listen(+env.BIND_PORT || 1337, env.BIND_ADDRESS || '::');
}
module.exports = { server, wss };