-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
ws.js
37 lines (31 loc) · 1.04 KB
/
ws.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
const WebSocket = require('ws');
const fs = require('fs');
function startServer() {
const wss = new WebSocket.Server({
port: 8579,
});
wss.on('connection', (ws) => {
const messages = JSON.parse(fs.readFileSync('./ws-messages/onConnect.json'));
messages.forEach((message) => {
ws.send(JSON.stringify(message));
});
ws.addEventListener('message', (message) => {
const msg = JSON.parse(message.data);
switch (msg.topic) {
case 'bridge/request/networkmap':
ws.send(fs.readFileSync('./ws-messages/networkMapRequest.json', 'utf8'));
break;
case 'bridge/request/touchlink/scan':
ws.send(fs.readFileSync('./ws-messages/onTouchlink.json', 'utf8'));
break;
default:
break;
}
});
});
console.log('started ws server');
}
if (require.main === module) {
startServer();
}
module.exports = { startServer };