1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00

UI autoreconnect and resume on lost connection

This commit is contained in:
AmateurAudioDude
2024-08-13 02:21:32 +10:00
committed by GitHub
parent f687a46916
commit 8676b866dc

View File

@@ -219,8 +219,47 @@ function sendPingRequest() {
.catch(error => { .catch(error => {
console.error('Error fetching ping:', error); console.error('Error fetching ping:', error);
}); });
// Automatic reconnection on WebSocket close
if (socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING) {
console.log("Main/UI attempting to reconnect...", socketAddress);
socket = new WebSocket(socketAddress);
socket.onopen = () => {
console.log("Main/UI reconnected successfully.");
};
socket.onmessage = (event) => {
handleWebSocketMessage(event);
};
socket.onerror = (error) => {
console.error("Main/UI WebSocket error during reconnection:", error);
};
socket.onclose = () => {
console.warn("Main/UI WebSocket closed during reconnection. Will attempt to reconnect...");
};
}
} }
// Automatic UI resume on WebSocket reconnect
function handleWebSocketMessage(event) {
if (event.data == 'KICK') {
console.log('Kick initiated.')
setTimeout(() => {
window.location.href = '/403';
}, 500);
return;
}
parsedData = JSON.parse(event.data);
updatePanels(parsedData);
const sum = signalData.reduce((acc, strNum) => acc + parseFloat(strNum), 0);
const averageSignal = sum / signalData.length;
data.push(averageSignal);
}
// Attach the message handler
socket.onmessage = handleWebSocketMessage;
function initCanvas(parsedData) { function initCanvas(parsedData) {
signalToggle = $("#signal-units-toggle"); signalToggle = $("#signal-units-toggle");
@@ -370,7 +409,7 @@ function updateCanvas(parsedData, signalChart) {
socket.onmessage = (event) => { socket.onmessage = (event) => {
if (event.data == 'KICK') { if (event.data == 'KICK') {
console.log('Kick iniitiated.') console.log('Kick initiated.')
setTimeout(() => { setTimeout(() => {
window.location.href = '/403'; window.location.href = '/403';
}, 500); }, 500);