forked from simplewebrtc/signalmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (53 loc) · 1.63 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
/*global console*/
var yetify = require('yetify'),
config = require('getconfig'),
uuid = require('node-uuid'),
io = require('socket.io').listen(config.server.port);
io.sockets.on('connection', function (client) {
// pass a message
client.on('message', function (details) {
var otherClient = io.sockets.sockets[details.to];
if (!otherClient) {
return;
}
delete details.to;
details.from = client.id;
otherClient.emit('message', details);
});
client.on('join', function (name) {
client.join(name);
io.sockets.in(name).emit('joined', {
room: name,
id: client.id
});
});
client.on('disconnect', function () {
var rooms = io.sockets.manager.roomClients[client.id];
for (var name in rooms) {
if (name) {
io.sockets.in(name.slice(1)).emit('left', {
room: name,
id: client.id
});
}
}
});
client.on('create', function (name, cb) {
if (arguments.length == 2) {
cb = (typeof cb == 'function') ? cb : function () {};
name = name || uuid();
} else {
cb = name;
name = uuid();
}
// check if exists
if (io.sockets.clients(name).length) {
cb('taken');
} else {
client.join(name);
if (cb) cb(null, name);
}
});
});
if (config.uid) process.setuid(config.uid);
console.log(yetify.logo() + ' -- signal master is running at: http://localhost:' + config.server.port);