forked from nurikk/zigbee2mqtt-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.js
40 lines (37 loc) · 1.14 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
38
39
40
const WebSocket = require("ws");
const zigbeeMessages = require("./ws-messages/zigbee.json");
const logMessages = require("./ws-messages/log.json");
const wss = new WebSocket.Server({
port: 8579
});
let messageNum = 0;
let intervalId = 0;
wss.on("connection", (ws) => {
ws.on("message", (message) => {
let messages = [];
const msg = JSON.parse(message);
switch (msg.category) {
case "log":
messages = logMessages;
break;
case "zigbee":
messages = zigbeeMessages;
break;
default:
break;
}
clearInterval(intervalId);
messageNum = 0;
console.log("Sending message", messageNum);
ws.send(JSON.stringify(messages[messageNum++]));
intervalId = setInterval(() => {
if (messageNum >= messages.length) {
messageNum = 0;
clearInterval(intervalId);
} else {
console.log("Sending message", messageNum);
ws.send(JSON.stringify(messages[messageNum++]));
}
}, 100);
});
});