forked from CNXTEoEorg/remotebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (36 loc) · 925 Bytes
/
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
var WSS = require('ws').Server;
var http = require('http');
var express = require('express');
module.exports = ({ port, state, app }) => {
app.set('view engine', 'pug');
app.get('/', (req, res) => {
var user = req.user;
var authed = req.isAuthenticated();
res.render('index', { user, authed });
});
app.use(express.static(__dirname + '/public'));
var server = http.createServer(app);
server.listen(port);
var verifyClient = app.verifyClient;
var wss = new WSS({
server,
verifyClient,
keepAlive: {
interval: 1000,
timeout: 5000
}
});
wss.on('connection', (socket) => {
console.log('client');
socket.send(state.getStatus());
socket.on('message', (status) => {
state.updateStatus(status);
});
});
state.on('change', ({ status }) => {
wss.clients.forEach((client) => {
client.send(status);
});
});
return server;
};