-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathrealTimeServer.js
59 lines (47 loc) · 1.84 KB
/
realTimeServer.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
(function(){
exports.initialize = function(io, socket ){
var room = null;
function log(){
var array = [">>> Message from server: "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
socket.on('message', function (message) {
log('Got message: ', message);
// For a real app, should be room only (not broadcast)
socket.broadcast.to(room).emit('message', message);
});
socket.on('create or join', function (_room) {
var numClients = io.sockets.clients(_room).length;
log('Room ' + _room + ' has ' + numClients + ' client(s)');
log('Request to create or join room: ', _room);
if (numClients == 0){
room = _room;
socket.join(room);
socket.emit('created', room);
} else if (numClients == 1) {
room = _room;
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else { // max two clients
socket.emit('full', _room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);
});
socket.on('makeoffer',function(desc){
socket.broadcast.to(room).emit('message', desc);
});
socket.on('disconnect', function () {
socket.broadcast.to(room).emit('bye', null);
socket.leave(room);
});
socket.on('tellbye', function () {
socket.broadcast.to(room).emit('bye', null);
socket.leave(room);
});
}
})();