-
Notifications
You must be signed in to change notification settings - Fork 0
/
monserve.js
42 lines (36 loc) · 1.1 KB
/
monserve.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
const express = require('express')
const app = express()
const conf = require('./conf.js')
const sb = require('spellbook')
const io = require('socket.io')({ transports: ['websocket'] })
var nodes = {}
app.use('/', express.static('public'))
app.listen(conf.http_port, () => {
console.log(`Web server started in http://localhost:${conf.http_port}`)
})
io.attach(conf.socket_port)
var intervals = {}
io.on('connection', (socket) => {
console.log(`-> conected: ${socket.id}`)
socket.on('stats', (data) => {
if (sb.empty(data.key) || sb.empty(data.hostname) || data.key !== conf.key) {
console.log(`x- kick: ${socket.id}`)
socket.disconnect()
} else {
delete(data.key)
nodes[socket.id] = data
}
})
socket.on('info', () => {
intervals[socket.id] = setInterval(() => {
io.to(socket.id).emit('stats', nodes)
}, 3000)
})
socket.on('disconnect', () => {
if (!sb.empty(nodes[socket.id])) delete(nodes[socket.id])
if (!sb.empty(intervals[socket.id])) {
clearInterval(intervals[socket.id])
}
console.log(`<- disconnected: ${socket.id}`)
})
})