/
app.js
63 lines (46 loc) · 1.31 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
var express = require('express');
var http = require('http');
var WS = require('ws').Server;
var fs = require('fs');
var app = express();
var server = http.createServer(app);
var ws = new WS({ server: server });
var js = fs.readFileSync('client.js');
var background = '#22112a';
function setBackground(color) {
if (!/^#?([0-9a-f]{6}|[0-9a-f]{3})$/i.test(color))
return false;
if (color[0] != '#')
color = '#' + color;
background = color;
ws.clients.forEach(function(client) {
client.send(background);
});
return true;
}
(function keepalive() {
setBackground(background);
setTimeout(keepalive, 15 * 1000);
})();
ws.on('connection', function(wc) {
wc.send(background);
wc.on('message', function(bg) {
setBackground(bg);
});
});
app.get('/', function(req, res) {
if (process.env.NODE_ENV == 'development')
js = fs.readFileSync('client.js');
res.set('Content-Type', 'text/html');
res.send('<!doctype html><style>html{background:' + background + '}</style><script>' + js + '</script>');
});
app.get('/get', function(req, res) {
res.json({ background: background });
});
app.all('/set', function(req, res) {
var bg = req.param('bg');
if (!setBackground(bg))
return res.json(400, { error: 'bad background' });
res.json({ message: 'background changed' });
});
server.listen(process.env.PORT || 9001);