-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
357 lines (320 loc) · 10.9 KB
/
server.js
File metadata and controls
357 lines (320 loc) · 10.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* Server module.
*
*
*/
'use strict';
const nodestatic = require('node-static');
const express = require('express');
const path = require('path');
const https = require('https');
const fs = require('fs');
const kurento = require('kurento-client');
const serverPort = 8443;
////////////////////////////////////////////////
// LOCAL
// const serverIpAddress = '0.0.0.0';
// const socketIoServer = 'https://localhost';
// const socketIoServer = 'https://192.168.16.211';
const kurento_uri = 'ws://192.168.0.20:8888/kurento';
////////////////////////////////////////////////
////////////////////////////////////////////////
// PRODUCTION
const serverIpAddress = '0.0.0.0';
const socketIoServer = 'fit5.fit-uet.tk';
// const kurento_uri = 'ws://localhost:8888/kurento';
////////////////////////////////////////////////
const pkey = fs.readFileSync('keys/key.pem');
const pcert = fs.readFileSync('keys/cert.pem');
const options = {
key: pkey,
cert: pcert
}
////////////////////////////////////////////////
// SETUP SERVER
////////////////////////////////////////////////
const app = express();
require('./router')(app, socketIoServer);
// Static content (css, js, .png, etc) is placed in /public
app.use(express.static(__dirname + '/public'));
// Location of our views
app.set('views', __dirname + '/views');
// Use ejs as our rendering engine
app.set('view engine', 'ejs');
// Tell Server that we are actually rendering HTML files through EJS.
app.engine('html', require('ejs').renderFile);
const sslServ = https.createServer(options, app).listen(serverPort, serverIpAddress, function () {
console.log("Express is running on port " + serverPort);
});
// const server = app.listen(serverPort, serverIpAddress, function () {
// console.log("Express is running on port " + serverPort);
// });
//random name
const usersDB = {
roonName: [
{
userName: "x",
participantID: "1"
},
]
}
const nameTemplates = [
'Chó', 'Mèo', 'Lợn', 'Gà', 'DOGE', 'Quạ', 'Chồn chồn', 'Gấu', 'Chim cánh cụt', 'Khung long', 'Tê tê', 'Chuồn chuồn', 'Cá sẫu', 'Cá đẹp', 'Cá mập', 'Mèo', 'Tu hú', 'Chim sẻ', 'Cu gáy', 'Chim cu', 'Đà điểu', 'Lười', 'Tinh tinh', 'Đười ươi'
]
const names = {
roomName: [
'Chó', 'Mèo', 'Lợn', 'Gà', 'DOGE', 'Quạ', 'Chồn chồn', 'Gấu', 'Chim cánh cụt', 'Khung long', 'Tê tê', 'Chuồn chuồn', 'Cá sẫu', 'Cá đẹp', 'Cá mập', 'Mèo', 'Tu hú', 'Chim sẻ', 'Cu gáy', 'Chim cu', 'Đà điểu', 'Lười', 'Tinh tinh', 'Đười ươi'
]
}
function getRandomName(tmpName, roomName) {
if (names[roomName]) {
const namesTmp = names[roomName].slice();
tmpName.forEach(element => {
namesTmp.splice(namesTmp.indexOf(element.userName), 1);
});
const name = namesTmp[Math.floor(Math.random() * namesTmp.length)]
return name;
} else {
names[roomName] = [
'Chó', 'Mèo', 'Lợn', 'Gà', 'DOGE', 'Quạ', 'Chồn chồn', 'Gấu', 'Chim cánh cụt', 'Khung long', 'Tê tê', 'Chuồn chuồn', 'Cá sẫu', 'Cá đẹp', 'Cá mập', 'Mèo', 'Tu hú', 'Chim sẻ', 'Cu gáy', 'Chim cu', 'Đà điểu', 'Lười', 'Tinh tinh', 'Đười ươi'
]
const namesTmp = names[roomName].slice();
tmpName.forEach(element => {
namesTmp.splice(namesTmp.indexOf(element.userName), 1);
});
const name = namesTmp[Math.floor(Math.random() * namesTmp.length)]
return name;
}
}
//end
const io = require('socket.io').listen(sslServ);
const sdpOfferCache = {};
let candidatesQueue = {};
////////////////////////////////////////////////
// EVENT HANDLERS
////////////////////////////////////////////////
io.sockets.on('connection', function (socket) {
function log() {
let array = [">>> Message from server: "];
for (let i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
socket.on('message', function (message) {
log('Got message: ', message);
socket.broadcast.to(socket.room).emit('message', message);
});
socket.on('disconnect', function () {
socket.broadcast.to(socket.room).emit('message', {
type: 'bye',
from: socket.participantID
});
});
socket.on('cli2kms', function (message) {
switch (message.type) {
// 1
case 'offer':
kurento(kurento_uri, (error, kurentoClient) => {
if (error) {
console.error(error);
}
else {
kurentoClient.create('MediaPipeline', (error, pipeline) => {
socket.pipeline = pipeline;
pipeline.create('WebRtcEndpoint', (error, webRtcEndpoint) => {
socket.webRtcEndpoint = webRtcEndpoint;
// Relay icecandidate from kurento server to client
webRtcEndpoint.on('OnIceCandidate', event => {
let candidate = kurento.getComplexType('IceCandidate')(event.candidate);
socket.emit('kms2cli', {
type: 'candidate',
candidate: candidate
});
});
// received sdpOffer and response sdpAnswer
// console.log('offer sdp', message.sdp);
webRtcEndpoint.processOffer(message.sdp, (error, sdpAnswer) => {
if (error) {
console.error(error);
}
else {
// 2
socket.emit('kms2cli', {
type: 'answer',
sdp: sdpAnswer
});
// Search icecandidate available
webRtcEndpoint.gatherCandidates(error => {
if (error) {
console.error(error);
return;
}
});
}
});
let numOfUserInRoom = io.sockets.clients(socket.room).length;
if (numOfUserInRoom <= 1) return;
io.sockets.clients(socket.room).forEach(client => {
if (client.participantID != socket.participantID) {
// old user as viewer
// 2.5
client.pipeline.create('WebRtcEndpoint', (error, webRtcEndpoint) => {
// client.streamInput[socket.participantID] = webRtcEndpoint;
socket.streamInput[client.participantID] = webRtcEndpoint;
webRtcEndpoint.on('OnIceCandidate', event => {
let candidate = kurento.getComplexType('IceCandidate')(event.candidate);
socket.emit('kms2cli', {
type: 'candidate',
candidate: candidate,
from: client.participantID
});
});
client.webRtcEndpoint.connect(webRtcEndpoint, error => {
console.error(error);
});
// webRtcEndpoint.connect(client.webRtcEndpoint, err => console.error(err));
webRtcEndpoint.generateOffer((error, sdp) => {
socket.emit('kms2cli', {
type: 'offer',
sdp: sdp,
from: client.participantID
});
webRtcEndpoint.gatherCandidates(error => {
console.error(error);
});
});
});
// new user as viewer
// 3
socket.pipeline.create('WebRtcEndpoint', (error, webRtcEndpoint) => {
// socket.streamInput[client.participantID] = webRtcEndpoint;
client.streamInput[socket.participantID] = webRtcEndpoint;
webRtcEndpoint.on('OnIceCandidate', event => {
let candidate = kurento.getComplexType('IceCandidate')(event.candidate);
client.emit('kms2cli', {
type: 'candidate',
candidate: candidate,
from: socket.participantID
});
});
socket.webRtcEndpoint.connect(webRtcEndpoint, error => {
console.error(error);
});
webRtcEndpoint.generateOffer((error, sdp) => {
client.emit('kms2cli', {
type: 'offer',
sdp: sdp,
from: socket.participantID
});
webRtcEndpoint.gatherCandidates(error => {
console.error(error);
});
});
});
}
});
});
});
}
});
break;
case 'answer':
io.sockets.clients(socket.room).forEach(client => {
if (client.participantID == message.from && client.streamInput && client.streamInput[message.to]) {
client.streamInput[message.to].processAnswer(message.sdp);
}
});
break;
case 'candidateUplink':
if (socket.webRtcEndpoint) {
socket.webRtcEndpoint.addIceCandidate(message.candidate);
}
// else {
// if (!candidatesQueue[socket.room]) {
// candidatesQueue[socket.room] = [];
// }
// candidatesQueue[socket.room].push(message.candidate);
// }
break;
case 'candidateDownlink':
io.sockets.clients(socket.room).forEach(client => {
if (client.participantID == message.from && client.streamInput && client.streamInput[message.to]) {
client.streamInput[message.to].addIceCandidate(message.candidate);
}
// else {
// if (!candidatesQueue[socket.room]) {
// candidatesQueue[socket.room] = [];
// }
// candidatesQueue[socket.room].push(message.candidate);
// }
});
break;
}
});
socket.on('create_or_join', function (message) {
let room = message.room;
socket.room = room;
socket.streamInput = {};
let participantID = message.from;
socket.participantID = participantID;
configNameSpaceChannel(participantID);
let numClients = io.sockets.clients(room).length;
log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room', room);
if (numClients == 0) {
socket.join(room);
socket.emit('created', room);
usersDB[room] = [];
//random name
const userTmp = usersDB[room].find(e => e.participantID === socket.participantID);
console.log(userTmp);
if (userTmp) {
socket.userName = userTmp.userName;
console.log("old");
} else {
console.log("new");
socket.userName = getRandomName(usersDB[room], room);
usersDB[room].push(socket);
}
} else {
//random name
const userTmp = usersDB[room].find(e => e.participantID === socket.participantID);
console.log(userTmp);
if (userTmp) {
socket.userName = userTmp.userName;
console.log("old");
} else {
console.log("new");
socket.userName = getRandomName(usersDB[room], room);
usersDB[room].push(socket);
}
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
}
socket.emit('create_or_join_success');
});
socket.on('send_message', function (message) {
let room = socket.room;
io.sockets.clients(room).forEach(client => {
if (client.participantID != socket.participantID) {
client.emit('new_message', JSON.stringify({
participantID: socket.participantID,
message: message,
userName: socket.userName
}));
}
});
});
// Setup a communication channel (namespace) to communicate with a given participant (participantID)
function configNameSpaceChannel(participantID) {
let socketNamespace = io.of('/' + participantID);
socketNamespace.on('connection', function (socket) {
socket.on('message', function (message) {
// Send message to everyone BUT sender
socket.broadcast.emit('message', message);
});
});
}
});