forked from Hemisphere-Project/bcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (50 loc) · 1.73 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// kademe-server
var lastCmd = {'action': 'phase', 'arg': 'intro', 'from': 'Bonnard'}
var book = []
var path = require('path')
var express = require('express')
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.use('/js', express.static(path.join(__dirname, 'www/js')))
app.use('/font', express.static(path.join(__dirname, 'www/font')))
app.use('/css', express.static(path.join(__dirname, 'www/css')))
app.use('/res', express.static(path.join(__dirname, 'www/res')))
app.use('/img', express.static(path.join(__dirname, 'www/img')))
app.use('/lib', express.static(path.join(__dirname, 'www/lib')))
app.get('/', function(req, res){
res.sendFile(__dirname + '/www/index.html');
});
io.on('connection', function(client)
{
console.log('a user connected');
// Once client introduce himself
client.on('iam', (name)=>{
// Setup client to correct phase
client.emit('cmd', lastCmd)
client.emit('allNames', book)
// Notify others
client['name'] = name
book.push(name)
io.emit('newName', name)
console.log('cli introduced as', name)
console.log('number of cli: ', book.length)
console.log('cli list: ', book)
})
// Client exit: remove from book and inform others
client.on('disconnect', function(){
console.log('user disconnected');
io.emit('goneName', client['name'])
var index = book.indexOf(client['name']);
if (index > -1) book.splice(index, 1);
});
// Relay cmd from Kontroler to all clients
client.on('cmd', (data)=>{
console.log('relay cmd', data);
if (data.action == 'phase') lastCmd = data // save activePhase
io.emit('cmd', data)
})
});
http.listen(8080, function(){
console.log('listening on *:8080');
});