mirror of
https://github.com/HendrikRauh/dmx-interface.git
synced 2025-07-12 00:15:44 +00:00
add WebSocket support for real-time status updates
This commit is contained in:
parent
c0da3a6af1
commit
e256d16f8d
8 changed files with 112 additions and 30 deletions
37
data/websocket.js
Normal file
37
data/websocket.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
const gateway = `ws://${window.location.hostname}/ws`;
|
||||
let ws;
|
||||
|
||||
let callbacks = {};
|
||||
|
||||
export function initWebSocket() {
|
||||
if (ws) return;
|
||||
|
||||
ws = new WebSocket(gateway);
|
||||
|
||||
ws.onopen = () => {
|
||||
console.info("WebSocket connection opened");
|
||||
};
|
||||
|
||||
ws.onclose = (event) => {
|
||||
console.info("WebSocket connection closed, reason:", event.reason);
|
||||
ws = null;
|
||||
};
|
||||
|
||||
ws.onerror = (event) => {
|
||||
console.warn("WebSocket encountered error, closing socket.", event);
|
||||
ws.close();
|
||||
ws = null;
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
const message = JSON.parse(event.data);
|
||||
console.log("received websocket data", message);
|
||||
if (message.type in callbacks) {
|
||||
callbacks[message.type](message.data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function registerCallback(type, callback) {
|
||||
callbacks[type] = callback;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue