Skip to content

Commit

Permalink
added setStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
gemi254 committed Jan 9, 2024
1 parent 9553870 commit b06972b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/controlAssistPMem.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,33 @@ const dbg = true;
let ws = null;
let hbTimer = null;
let refreshInterval = 15000;
let wsStatus = null;

// close web socket on leaving page
window.addEventListener('beforeunload', function (event) {
if (ws) closeWS();
});

function setStatus(msg){
if(!wsStatus) return;
wsStatus.innerHTML = msg
if(dbg) console.log(msg)
var event = new Event('change');
wsStatus.dispatchEvent(event);
}

// Websocket handling
function initWebSocket() {
console.log("Connect to: " + wsServer);
function initWebSocket() {
wsStatus = document.getElementById("wsStatus")
setStatus("Connecting to: " + wsServer);
ws = new WebSocket(wsServer);
ws.onopen = onWsOpen;
ws.onclose = onWsClose;
ws.onmessage = onWsMessage;
ws.onerror = onWsError;
}
async function closeWS() {
setStatus("Disconnected")
ws.send('0\tC');
await sleep(500);
ws.close();
Expand All @@ -104,7 +115,7 @@ function wsHeartBeat() {
}
// Connected
function onWsOpen(event) {
if(dbg) console.log("onWsOpen");
setStatus("Connected")
wsHeartBeat();
}
// Handle websocket messages
Expand Down Expand Up @@ -146,11 +157,11 @@ function onWsMessage(messageEvent) {
}
}
function onWsError(event) {
console.log("WS Error: " + event.code);
setStatus("Error: " + event.code)
}

function onWsClose(event) {
console.log("Discon: " + event.code + ' - ' + event.reason);
setStatus("Disconnect: " + event.code + ' - ' + event.reason);
ws = null;
// event.codes:
// 1006 if server not available, or another web page is already open
Expand Down

0 comments on commit b06972b

Please sign in to comment.