-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathapp.js
More file actions
92 lines (73 loc) · 2.05 KB
/
app.js
File metadata and controls
92 lines (73 loc) · 2.05 KB
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
89
90
91
92
const express = require('express');
const path = require('path');
const app = express();
const WebSocketServer = require('ws').Server;
const server = require('http').createServer(app);
const wss = new WebSocketServer({ server });
const port = 3000;
// Serve js files
app.use('/js', express.static(path.join(__dirname, 'ui/js/')));
// Serve css files
app.use('/css', express.static(path.join(__dirname, 'ui/css/')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/ui/html/index.html'));
});
function handleQuery(query, cb) {
cb('Awesome');
}
/**
* Client Counter
* Count the number of active connections
* @type {Number}
*/
let cc = 0;
wss.on('connection', function connection(ws) {
console.log('client connections: ', ++cc);
ws.on('message', function incoming(message) {
try {
const { payload, type } = JSON.parse(message);
switch(type) {
case 'query':
handleQuery(payload, (response) => {
ws.send(JSON.stringify({type: 'queryResponse', payload: response}));
});
return;
default:
console.log(message);
}
} catch(e) {
console.error('Error from message: ', e);
}
});
// Send welcome message on each connection
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify({type: 'connected', payload: 'Welcome!'}));
}
ws.on('close', function close() {
--cc;
if (cc === 0) {
clearInterval(pingInterval);
}
console.log('disconnected');
});
ws.on('error', function error() {
--cc;
console.log('error');
});
});
const pingPayload = JSON.stringify({type: 'ping'});
// Keep the connection alive
let pingInterval = setInterval(() => {
wss.broadcast(pingPayload);
}, 1 * 5000);
/**
* Broadcast data to all connected clients
* @param {Object} data
* @void
*/
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
client.send(data);
});
};
server.listen(port, () => console.log(`App listening on port ${port}!`))