-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket-server.js
44 lines (35 loc) · 1.12 KB
/
websocket-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
40
41
42
43
44
let fs = require('fs')
let express = require('express');
let app = express();
let expressWs = require('express-ws')(app);
app.use(function (req, res, next) {
console.log('middleware');
req.testing = 'testing';
return next();
});
app.get('/', function (req, res, next) {
console.log('get route', req.testing);
res.end();
});
let rawData = fs.readFileSync('src/examples/websocket-example-data.json');
let exampleData = JSON.parse(rawData);
app.ws('/', function (ws, req) {
ws.on('message', function (msg) {
ws.send(JSON.stringify(exampleData.firstData));
});
ws.on('close', function () {
clearInterval(intervalCounter);
});
let updatedData = exampleData.others;
let counter = 0;
let intervalCounter = setInterval(function () {
if (typeof (updatedData[counter]) === "undefined") {
clearInterval(intervalCounter);
return;
}
ws.send(JSON.stringify(updatedData[counter]));
counter++;
}, 1000);
console.log('socket', req.testing);
});
app.listen(3001, () => console.log('Example webscoket app listening on port 3001!'));