forked from simplewebrtc/signalmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets.js
151 lines (130 loc) · 4.68 KB
/
sockets.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var socketIO = require('socket.io'),
uuid = require('node-uuid'),
crypto = require('crypto');
module.exports = function (server, config) {
var io = socketIO.listen(server);
io.sockets.on('connection', function (client) {
client.resources = {
screen: false,
video: true,
audio: false
};
// pass a message to another id
client.on('message', function (details) {
if (!details) return;
var otherClient = io.to(details.to);
if (!otherClient) return;
details.from = client.id;
otherClient.emit('message', details);
});
client.on('shareScreen', function () {
client.resources.screen = true;
});
client.on('unshareScreen', function (type) {
client.resources.screen = false;
removeFeed('screen');
});
client.on('join', join);
function removeFeed(type) {
if (client.room) {
io.sockets.in(client.room).emit('remove', {
id: client.id,
type: type
});
if (!type) {
client.leave(client.room);
client.room = undefined;
}
}
}
function join(name, cb) {
// sanity check
if (typeof name !== 'string') return;
// check if maximum number of clients reached
if (config.rooms && config.rooms.maxClients > 0 &&
clientsInRoom(name) >= config.rooms.maxClients) {
safeCb(cb)('full');
return;
}
// leave any existing rooms
removeFeed();
safeCb(cb)(null, describeRoom(name));
client.join(name);
client.room = name;
}
// we don't want to pass "leave" directly because the
// event type string of "socket end" gets passed too.
client.on('disconnect', function () {
removeFeed();
});
client.on('leave', function () {
removeFeed();
});
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
var room = io.nsps['/'].adapter.rooms[name];
if (room && room.length) {
safeCb(cb)('taken');
} else {
join(name);
safeCb(cb)(null, name);
}
});
// support for logging full webrtc traces to stdout
// useful for large-scale error monitoring
client.on('trace', function (data) {
console.log('trace', JSON.stringify(
[data.type, data.session, data.prefix, data.peer, data.time, data.value]
));
});
// tell client about stun and turn servers and generate nonces
client.emit('stunservers', config.stunservers || []);
// create shared secret nonces for TURN authentication
// the process is described in draft-uberti-behave-turn-rest
var credentials = [];
// allow selectively vending turn credentials based on origin.
var origin = client.handshake.headers.origin;
if (!config.turnorigins || config.turnorigins.indexOf(origin) !== -1) {
config.turnservers.forEach(function (server) {
var hmac = crypto.createHmac('sha1', server.secret);
// default to 86400 seconds timeout unless specified
var username = Math.floor(new Date().getTime() / 1000) + (parseInt(server.expiry || 86400, 10)) + "";
hmac.update(username);
credentials.push({
username: username,
credential: hmac.digest('base64'),
urls: server.urls || server.url
});
});
}
client.emit('turnservers', credentials);
});
function describeRoom(name) {
var adapter = io.nsps['/'].adapter;
var clients = adapter.rooms[name] || {};
var result = {
clients: {}
};
Object.keys(clients).forEach(function (id) {
result.clients[id] = adapter.nsp.connected[id].resources;
});
return result;
}
function clientsInRoom(name) {
return io.sockets.clients(name).length;
}
};
function safeCb(cb) {
if (typeof cb === 'function') {
return cb;
} else {
return function () {};
}
}