-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
39 lines (36 loc) · 1 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
36
37
38
39
let clients = [];
const express = require('express');
const port = process.env.PORT || 1337;
const bodyParser = require('body-parser');
const moment = require('moment');
const app = express()
.use(express.static(__dirname + '/public'))
.use(bodyParser.urlencoded({extended:true}))
.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html' )
})
.post('/data', (req, res) => {
const reqID = req.body.id;
const reqValue = req.body.value;
if(reqID && reqValue) {
const data = {
id: reqID,
value: reqValue,
timeStamp: moment().format('HH:mm')
}
clients.forEach(client => client.send(JSON.stringify(data)));
}
res.end();
})
.listen(port, () => console.log('Running on ' + port));
const WebSocket = require('ws');
const WebSocketServer = WebSocket.Server;
const wss = new WebSocketServer({server: app});
wss.on('connection', (ws) => {
clients.push(ws);
ws.on('close', (code, message) => {
let idx = clients.indexOf(ws);
if (idx >= 0)
clients.splice(idx, 1);
});
});