-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
35 lines (29 loc) · 1.14 KB
/
server.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
const assert = require('assert');
const WebSocket = require('ws');
const { spawn } = require('child_process');
const PORT = process.env.PORT || 8888;
const IP_OR_DOMAIN = process.env.PINGDOMAIN || '8.8.8.8';
const ENVIRONMENT = process.env.ENV || 'development';
if (ENVIRONMENT === 'production') console.log = () => {};
const child = spawn('ping', [IP_OR_DOMAIN]);
const wss = new WebSocket.Server({ port: PORT });
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
// data = data.trim(); // just to strip the trailing newline after a ping command.
wss.broadcast({ type: 'ping_response', data });
console.log(data);
});
// Broadcast to all.
wss.broadcast = function broadcast(payload) {
assert.ok(payload.data, 'Payload contains a `data` property.');
assert.ok(payload.type, 'Payload contains a `type` property.');
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(payload));
}
});
};
wss.on('connection', function connection(ws) {
console.log('client connected');
ws.send(JSON.stringify({ type: 'ping_connection', data: IP_OR_DOMAIN }));
});