-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
66 lines (52 loc) · 1.84 KB
/
app.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
const http = require('http');
const express = require('express');
const ws = require('ws');
const routes = require('./server/routes');
const api = require('./server/api');
const Response = require('./server/response');
//Port settings
const webPort = 5000;
//Setup the web and websocket servers
const app = express();
const server = http.createServer(app);
const wss = new ws.Server({ server: server });
//Expose the public folder
app.use(express.static('public'));
//Bring in the routes
app.use('/', routes);
//Set up the websocket listener
wss.on('connection', (ws) => {
console.log('Connection Established:');
console.log(ws.upgradeReq.headers);
console.log('\n');
ws.on('message', (raw) => {
let message;
try {
message = JSON.parse(raw);
} catch (err) {
return console.error("Error parsing message: %s\n", raw);
}
if (message && message.call) {
//keep ping request as close to the "metal" as possible (should be a separate server in production)
if (message.call === "ping"){
ws.send("pong" + message.stamp);
return;
}
//Process general message
console.log('received: %s\n', raw);
if (!api[message.call])
return;
let args = message.args || [];
args.unshift(new Response(ws, message));
args.unshift(ws);
api[message.call](...args);
}
});
ws.on('close', (status, clientMsg) => {
console.log(`Client disconnected (${status}) with message: ${clientMsg}\n`);
});
});
//Start the server
server.listen(process.env.PORT || webPort, () => {
console.log("Listening on %j\n", server.address());
});