-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (40 loc) · 1.34 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
40
41
42
43
44
45
46
47
48
49
50
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var ioMiddleware = require('socketio-wildcard')();
io.use(ioMiddleware);
var SIMULATOR_ENABLE = true;
const SIMULATOR_DELAY = 5000;
//var LOG_RECEIVE_FROM_DEVICE_EVENT = 'device-logs';
var LOG_RECEIVE_FROM_DEVICE_EVENT = '*';
var LOG_PUBLISH_TO_WEB_CLIENT_EVENT = 'published-logs';
var port = process.env.PORT || 3001;
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
server.listen(port, function () {
console.log('Socket Server listening at port %d', port);
});
io.on('connection', function (socket) {
console.log('A Client has been Connected!!');
socket.on(LOG_RECEIVE_FROM_DEVICE_EVENT, function (data) {
console.log('Request received!', data);
socket.broadcast.emit(LOG_PUBLISH_TO_WEB_CLIENT_EVENT, data);
});
socket.on('disconnect', function () {
console.log('A Client has been Disconnected!');
socket.broadcast.emit('disconnected', {});
});
// Simulator
(() => {
if (SIMULATOR_ENABLE) {
console.log('Simulating logs (ONCE)...');
var count = 0;
setTimeout(() => {
socket.broadcast.emit(LOG_PUBLISH_TO_WEB_CLIENT_EVENT, { data: 'Simulated Data!', count: ++count });
}, SIMULATOR_DELAY);
}
})();
});