-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (54 loc) · 1.91 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
function handler (req, res) {
res.writeHead(200);
res.end();
}
io.set('log level', 2);
var connectedClients = 0;
var yesFromClients = 0;
var ackFromClients = 0;
io.sockets.on('connection', function (socket) {
socket.on('coordinator',function(data){
socket.join('coordinators');
console.log(socket.id + ": coordinator");
});
socket.on('prepare',function(data){
io.sockets.in('clients').emit('prepare',data);
console.log(socket.id + ": prepare");
});
socket.on('commit',function(data){
io.sockets.in('clients').emit('commit',data);
console.log(socket.id + ": commit");
});
socket.on('client',function(data){
socket.join('clients');
connectedClients++;
io.sockets.in('coordinators').emit('newClient',{client: socket.id});
console.log(socket.id + ": client");
socket.on('yes',function(data){
yesFromClients++;
if(yesFromClients == connectedClients){
io.sockets.in('clients').emit('commit',data);
}
io.sockets.in('coordinators').emit('yes',{client: socket.id});
console.log(socket.id + ": yes");
});
socket.on('ack',function(data){
io.sockets.in('coordinators').emit('ack',{client: socket.id});
ackFromClients++;
if(ackFromClients == connectedClients){
ackFromClients = 0;
yesFromClients = 0;
}
console.log(socket.id + ": ack");
});
socket.on('disconnect',function(data){
io.sockets.in('coordinators').emit('clientDisconnect',{client: socket.id});
console.log(socket.id + ": disconnect");
connectedClients--;
});
});
});