-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVoiceServer.js
56 lines (45 loc) · 1.32 KB
/
VoiceServer.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
const config = require('./Configuration.json');
const logger = require('./Logger.js');
const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
const wss = https.createServer({
cert: fs.readFileSync(config.ws.certPath),
key: fs.readFileSync(config.ws.keyPath),
});
const voiceSocket = new WebSocket.Server({ server: wss });
global.voiceServer = voiceSocket;
voiceSocket.on('connection', async (client, req) => {
client.clientId = generateClientId();
logger.info('Connection from {0} (Assigned client-id {1})', client._socket.remoteAddress, client.clientId);
client.on('message', (data) => {
voiceSocket.clients.forEach(ws => {
if (ws.readyState === WebSocket.OPEN && ws.clientId !== client.clientId) {
ws.send(data, {
binary: true
});
}
});
});
});
function generateClientId () {
let largestId = 1;
voiceSocket.clients.forEach(ws => {
if (ws.clientId && ws.clientId > largestId) {
largestId = ws.clientId;
}
});
return largestId + 1;
}
function start () {
wss.listen(config.voicews.port, () => {
logger.info('[VWS] Voice WebSocket started on port {0}', config.voicews.port);
});
}
function shutdown () {
wss.close();
}
module.exports = {
start,
shutdown
};