forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
executable file
·44 lines (33 loc) · 1.08 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
const net = require('net');
const Client = require('./client');
const SESSION_ID_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789';
const SESSION_ID_LENGTH = 12;
class NMServer extends net.Server {
constructor(opts) {
super(opts);
this.conns = {};
this.producers = {};
this.on('connection', socket => {
const id = this.generateNewSessionID();
const client = new Client(id, socket, this.conns, this.producers);
client.on('error', err => socket.destroy(err));
socket.on('data', data => client.bp.push(data));
socket.on('end', () => client.stop());
socket.on('error', err => client.emit('error', err));
this.emit('client', client);
client.run();
});
}
generateNewSessionID() {
let sessionId;
do {
sessionId = '';
for (let i = 0; i < SESSION_ID_LENGTH; i++) {
const charIndex = (Math.random() * SESSION_ID_CHARS.length) | 0;
sessionId += SESSION_ID_CHARS.charAt(charIndex);
}
} while (this.conns.hasOwnProperty(sessionId));
return sessionId;
}
}
module.exports = NMServer;