-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwssServer.js
More file actions
51 lines (44 loc) · 1.56 KB
/
Copy pathwssServer.js
File metadata and controls
51 lines (44 loc) · 1.56 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
const WebSocket = require('ws');
var wss;
module.exports.WebsocketServer = () => {
global.audioWorker.on('message', (msg) => {
wss.broadcast(msg);
});
global.audioWorker.send({ command: 'getStatus' });
let createServer = (server) => {
wss = new WebSocket.Server({ server });
wss.on('error', () => console.log('wss errored'));
wss.broadcast = broadcast = (data) => {
wss.clients.forEach(each = (client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
};
wss.on('connection', connection = (client) => {
client.on('message', incoming = (msg) => {
if (msg == 'play') {
global.audioWorker.send({ command: 'play' });
} else if (msg == 'stop') {
global.audioWorker.send({ command: 'stop' });
} else if (msg == 'startRecord') {
global.audioWorker.send({ command: 'startRecord' });
} else if (msg == 'stopRecord') {
global.audioWorker.send({ command: 'stopRecord' });
} else if (msg == 'getStatus') {
global.audioWorker.send({ command: 'getStatus' });
}
});
client.on('error', () => {
console.log('client error');
});
});
};
let get = () => {
return wss;
};
return {
createServer: createServer,
get: get
};
};