-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (40 loc) · 1.14 KB
/
index.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
var express = require('express')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const EMIT_KEY = process.env.EMIT_KEY;
app.use('/websocket', express.static('public'));
app.get('/websocket', function(req, res){
res.sendfile('index.html');
});
app.get('/websocket/monitor', function(req, res){
res.sendfile('monitor.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('reset', function(){
console.log('reset');
io.emit('reset');
});
socket.on('init', function(msg){
console.log('init: ' + JSON.stringify(msg));
if(msg && msg.hasOwnProperty('key') && msg.key == EMIT_KEY){
delete msg.key;
io.emit('init', msg);
}
});
socket.on('judged', function(msg){
console.log('judged: ' + JSON.stringify(msg));
if(msg.hasOwnProperty('key') && msg.key == EMIT_KEY){
delete msg.key;
io.emit('judged', msg);
}
});
});
var port = process.env.PORT || 3000;
http.listen(port, function(){
console.log('listening on *:',port);
});