70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
|
// websocket.js
|
||
|
let websocket;
|
||
|
|
||
|
export function connectWebSocket(url) {
|
||
|
websocket = new WebSocket(url);
|
||
|
|
||
|
websocket.onopen = () => {
|
||
|
console.log("WebSocket connection established");
|
||
|
};
|
||
|
|
||
|
websocket.onclose = () => {
|
||
|
console.log("QLC+ connection is closed. Reconnect will be attempted in 1 second.");
|
||
|
setTimeout(() => {
|
||
|
connectWebSocket(url);
|
||
|
}, 1000);
|
||
|
};
|
||
|
|
||
|
websocket.onerror = (err) => {
|
||
|
console.error("QLC+ connection encountered error. Closing socket", err);
|
||
|
websocket.close();
|
||
|
};
|
||
|
|
||
|
websocket.onmessage = (ev) => {
|
||
|
const message = ev.data.toString();
|
||
|
const msgParams = message.split("|");
|
||
|
|
||
|
if (msgParams[1] === "BUTTON") {
|
||
|
//wsSetButtonState(msgParams[0], msgParams[2]);
|
||
|
} else if (msgParams[1] === "SLIDER") {
|
||
|
wsSetSliderValue(msgParams[0], msgParams[2], msgParams[3]);
|
||
|
} else if (msgParams[1] === "AUDIOTRIGGERS") {
|
||
|
wsSetAudioTriggersEnabled(msgParams[0], msgParams[2]);
|
||
|
} else if (msgParams[1] === "CUE") {
|
||
|
wsSetCueIndex(msgParams[0], msgParams[2]);
|
||
|
} else if (msgParams[1] === "CLOCK") {
|
||
|
wsUpdateClockTime(msgParams[0], msgParams[2]);
|
||
|
} else if (msgParams[1] === "FRAME") {
|
||
|
setFramePage(msgParams[0], msgParams[2]);
|
||
|
} else if (msgParams[1] === "getWidgetsList") {
|
||
|
console.log(msgParams)
|
||
|
}
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
export function sendButtonPress(id, state) {
|
||
|
if (state == 1){
|
||
|
websocket.send(id + "|255");
|
||
|
// Quand un bouton est pressé, envoyez un message au serveur
|
||
|
console.log(`Button ${id} pressed and sent to server`);
|
||
|
}
|
||
|
if (state == 0){
|
||
|
websocket.send(id + "|0");
|
||
|
// Quand un bouton est pressé, envoyez un message au serveur
|
||
|
console.log(`Button ${id} released and sent to server`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function sendCMD(cmd) {
|
||
|
websocket.send("QLC+CMD|" + cmd);
|
||
|
}
|
||
|
|
||
|
export function requestAPI(cmd)
|
||
|
{
|
||
|
if (isConnected === true)
|
||
|
websocket.send("QLC+API|" + cmd);
|
||
|
else
|
||
|
alert("You must connect to QLC+ WebSocket first!");
|
||
|
}
|