-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketManager.js
64 lines (56 loc) · 1.7 KB
/
socketManager.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
57
58
59
60
61
62
63
64
const WebSocket = require("ws");
const {getAuthHeader} = require("./client");
const Max = require("max-api");
const currentlyRunning = new Map();
const currentlySubscribed = new Map();
const connectWebSocket = () => {
const socket = new WebSocket("wss://visuals.madzoo.events/api/socket", {
headers: {
"Authorization": getAuthHeader()
}
});
socket.on("open", () => Max.post("Connected to the websocket"));
socket.on("message", (data) => handleSocketMessage(JSON.parse(data)));
};
const handleSocketMessage = (message) => {
const {effectUpdate, clockUpdate} = message;
if (effectUpdate) {
const {id, action, errorMessage, stopAll} = effectUpdate;
if (!errorMessage) {
if (stopAll) {
const {stopEffects, detachClocks} = stopAll;
if (stopEffects) {
currentlyRunning.clear();
}
if (detachClocks) {
currentlySubscribed.clear();
}
} else {
if (action === "start") {
currentlyRunning.set(id, true);
} else if (action === "stop") {
currentlyRunning.delete(id);
}
}
}
}
if (clockUpdate) {
const {id, action} = clockUpdate;
if (action === "subscribe") {
currentlySubscribed.set(id, true);
} else {
currentlySubscribed.delete(id);
}
}
};
const isRunning = (id) => {
return currentlyRunning.has(id);
};
const isSubscribed = (id) => {
return currentlySubscribed.has(id);
};
module.exports = {
connectWebSocket,
isRunning,
isSubscribed
};