-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
45 lines (36 loc) · 1.17 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
var express = require('express')
, io = require('socket.io');
var app = express();
app.configure(function() {
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
});
var server = app.listen('4000')
, io = io.listen(server);
io.sockets.on('connection', function(socket) {
io.sockets.emit('total', io.sockets.clients().length);
// Notify everybody of the new client
io.sockets.clients().forEach(function(s) {
s.get('coordinates', function(err, coords) {
if(coords != null) {
socket.emit('new client', s.id, coords);
}
});
});
// Notify everybody of the new client
socket.on('new client', function(coords) {
socket.set('coordinates', coords, function() {
socket.broadcast.emit('new client', socket.id, coords);
});
});
//Notify everybody of disconnection
socket.on('disconnect', function() {
io.sockets.emit('client disconnected', socket.id);
io.sockets.emit('total', io.sockets.clients().length - 1);
});
//Exchange signals
socket.on('signal', function(id, message) {
io.sockets.socket(id).emit('signal', socket.id, message);
});
});
console.log('CallMap running at port 4000');