-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 1.68 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
const WebSocket = require('ws'),
glob = require('glob'),
http = require('http'),
path = require('path'),
url = require('url'),
debug = require('debug')('server'),
// instances
server = http.createServer(),
registeredPaths = {},
port = process.env.PORT || 3000;
// register generators
glob.sync('./generators/*.js').forEach((generator) => {
// skip base generator
if (generator === './generators/generator.js') {
return;
}
// path is '/foo'
const moduleName = path.basename(generator, '.js');
registeredPaths[`/${moduleName}`] = new (require(path.resolve(generator)))();
debug(`Registered module /${moduleName}`);
});
server.on('upgrade', (request, socket, head) => {
const pathName = url.parse(request.url).pathname;
debug(`trying to upgrade ${pathName}`);
if (registeredPaths[pathName]) {
debug(`found the upgradable path for ${pathName}`)
registeredPaths[pathName].handle(request, socket, head);
} else {
debug(`${pathName} is not found`);
socket.destroy();
}
});
server.listen(port, () => {
debug(`Listening on ${port}`);
});
process.on('uncaughtException', (err) => {
debug('Whoops! Something went wrong!');
if (err) {
debug(err.stack);
}
});
// // heartbeat
// function noop() {}
// function heartbeat() {
// this.isAlive = true;
// }
// function should_chuck_kill_it(client) {
// if (!client.isAlive) {
// return client.terminate();
// }
// client.isAlive = false;
// client.ping(noop);
// }
// wss.on('connection', (ws) => {
// ws.isAlive = true;
// ws.on('pong', heartbeat);
// });
// const intervalId = setInterval(() => {
// wss.clients.forEach((client) => should_chuck_kill_it(client));
// }, 30000);