-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockets.js
More file actions
180 lines (150 loc) · 5.75 KB
/
sockets.js
File metadata and controls
180 lines (150 loc) · 5.75 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var socketIO = require('socket.io'),
uuid = require('node-uuid'),
crypto = require('crypto');
module.exports = function (server, config) {
var io = socketIO.listen(server);
var custom = require('./socketsCustom')(io);
var settingsLoader = require('./settings.js');
if (config.logLevel) {
// https://github.com/Automattic/socket.io/wiki/Configuring-Socket.IO
// io.set('log level', config.logLevel);
}
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('fetchAvailableRooms', function(cb) {
var rooms = custom.getRooms();
safeCb(cb)(rooms);
});
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(roomName, pwd, cb) {
// sanity check
if (typeof roomName !== 'string') return;
if (!custom.roomExists(roomName) && !custom.isLecturer(client.id, roomName)) {
safeCb(cb)(settingsLoader.getErrNoLecturerInRoom());
return;
}
if (!custom.isRoomPwdCorrect(roomName, pwd)) {
safeCb(cb)(settingsLoader.getErrWrongRoomPassword());
return;
}
if (custom.isRoomFull(config, roomName, cb) ){
safeCb(cb)('full');
return;
}
// leave any existing rooms
removeFeed();
safeCb(cb)(null, describeRoom(roomName));
client.join(roomName);
client.room = roomName;
}
// we don't want to pass "leave" directly because the
// event type string of "socket end" gets passed too.
client.on('disconnect', function () {
custom.removeRoomOnLecturerDisconnect(client.id, client.room);
removeFeed();
});
client.on('leave', function () {
custom.removeRoomOnLecturerDisconnect(client.id, client.room);
removeFeed();
});
client.on('create', function (roomName, adminPwd, roomPwd, cb) {
function createRoom() {
custom.createRoom(roomName, adminPwd, roomPwd, client);
join(roomName, roomPwd);
safeCb(cb)(null, roomName);
}
if (custom.roomExistsAsSocket(roomName) && !custom.isAdminPwdCorrect(roomName, adminPwd)) {
safeCb(cb)(settingsLoader.getErrWrongAdminPassword());
} else {
createRoom();
}
});
// 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) + (server.expiry || 86400) + "";
hmac.update(username);
//credentials.push({
// username: username,
// credential: hmac.digest('base64'),
// urls: server.urls || server.url
//});
credentials.push({
username: 'user',
credential: 'pwd',
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: {},
lecturerId: custom.getLecturerPeerOfRoom(name).id
};
Object.keys(clients).forEach(function (id) {
// resources:{audio: false, screen: false, video: true}
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 () {};
}
}