-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
86 lines (70 loc) · 1.88 KB
/
Copy pathserver.js
File metadata and controls
86 lines (70 loc) · 1.88 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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const appStatus = require('./app-status');
let clients = [];
let history = [];
let initialized = false;
let emiting = false;
const maxHistorySeconds = 60 * 30;
io.on('connect', socket => {
clients.push(socket);
console.log(`socket connected, total ${clients.length}`);
socket.on('req_full_history', data => {
let resHistory = [];
history.forEach(elem => {
resHistory.push(elem[data.appIndex]);
});
socket.emit('res_full_history', { appHistory: resHistory });
});
socket.on('req_last_history_elem', data => {
appFocusIndex = data.appIndex;
});
socket.on('disconnect', () => {
clients.splice(clients.indexOf(socket), 1);
console.log(`socket disconnected, total ${clients.length}`);
});
});
setInterval(() => {
if (initialized === false) {
while (history.length < maxHistorySeconds - 1) {
history.push(
appStatus.generateAppStatusData(
history,
maxHistorySeconds,
history.length
).detail
);
}
initialized = true;
}
current = appStatus.generateAppStatusData(history);
history.push(current.detail);
if (history.length > maxHistorySeconds) {
history.splice(0, 1);
}
if (clients.length > 0) {
emiting = true;
io.emit('apps_status', {
current: current
});
io.emit('res_last_history_elem', {
latestHistory: history[history.length - 1]
});
} else {
emiting = false;
}
const date = new Date();
console.log(
`[${date.toLocaleDateString()} ${date.toLocaleTimeString()}] - ${
emiting
? `emiting, socket count: ${clients.length}`
: 'no socket connected'
}`
);
}, 1000);
const port = 8000;
http.listen(port, () => {
console.log(`listening on port ${port}`);
});