From 53609302676f6c894d296e2d105eb40398e352b8 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Tue, 14 Jul 2026 09:33:24 +0200 Subject: [PATCH] ping in ws --- server/web.js | 6 ++++ web/js/main.js | 94 +++++++++----------------------------------------- 2 files changed, 23 insertions(+), 77 deletions(-) diff --git a/server/web.js b/server/web.js index 8fd0026..bbb21fc 100644 --- a/server/web.js +++ b/server/web.js @@ -110,6 +110,12 @@ wss.on('connection', (ws, request) => { ws.on('message', (message) => { const command = helpers.antispamProtection(message, clientIp, ws, userCommands, lastWarn, userCommandHistory, '18', 'text', 16 * 1024); + if(!command) return; + + if(command.startsWith("PING")) { + ws.send(command); + return; + } if (!clientIp.includes("127.0.0.1")) { if (((command.startsWith('X') || command.startsWith('Y')) && !helpers.isAdmin(req)) || diff --git a/web/js/main.js b/web/js/main.js index edce891..310b798 100644 --- a/web/js/main.js +++ b/web/js/main.js @@ -9,7 +9,6 @@ let lastReconnectAttempt = 0; let messageCounter = 0; // Count for WebSocket data length returning 0 let messageData = 800; // Initial value anything above 0 let messageLength = 800; // Retain value of messageData until value is updated -let pingTimeLimit = false; // WebSocket becomes unresponsive with high ping const europe_programmes = [ "No PTY", "News", "Current Affairs", "Info", @@ -292,66 +291,10 @@ function getServerTime() { } function sendPingRequest() { - const timeoutDuration = 5000; - const startTime = new Date().getTime(); - - const fetchWithTimeout = (url, options, timeout = timeoutDuration) => { - return new Promise((resolve, reject) => { - const timerTimeout = setTimeout(() => { - reject(new Error('Request timed out')); - }, timeout); - - fetch(url, options) - .then(response => { - clearTimeout(timerTimeout); - resolve(response); - }) - .catch(error => { - clearTimeout(timerTimeout); - reject(error); - }); - }); - }; - - fetchWithTimeout('./ping', { cache: 'no-store' }, timeoutDuration) - .then(response => { - const endTime = new Date().getTime(); - const pingTime = endTime - startTime; - $('#current-ping').text(`Ping: ${pingTime}ms`); - pingTimeLimit = false; - }) - .catch(error => { - console.warn('Ping request failed'); - $('#current-ping').text(`Ping: unknown`); - if (!pingTimeLimit) { // Force reconnection as WebSocket could be unresponsive even though it's reported as OPEN - if (messageLength === 0) window.socket.close(1000, 'Normal closure'); - if (connectionLost) sendToast('warning', 'Connection lost', 'Attempting to reconnect...', false, false); - console.log("Reconnecting due to high ping..."); - pingTimeLimit = true; - } - }); - - function handleMessage(message) { - messageData = JSON.parse(message.data.length); - socket.removeEventListener('message', handleMessage); - } - socket.addEventListener('message', handleMessage); - messageLength = messageData; - messageData = 0; - - // Force reconnection if no WebSocket data after several queries - if (messageLength === 0) { - messageCounter++; - if (messageCounter === 5) { - messageCounter = 0; - window.socket.close(1000, 'Normal closure'); - if (connectionLost) sendToast('warning', 'Connection lost', 'Attempting to reconnect...', false, false); - console.log("Reconnecting due to no data received..."); - } - } else messageCounter = 0; - - // Automatic reconnection on WebSocket close with cooldown const now = Date.now(); + + if (socket.readyState === WebSocket.OPEN) socket.send(`PING ${now}`); + if ( (socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING) && (now - lastReconnectAttempt > TIMEOUT_DURATION) @@ -371,37 +314,35 @@ function sendPingRequest() { console.warn("Main/UI WebSocket closed during reconnection. Will attempt to reconnect..."); }; } - if (connectionLost) { - if (dataTimeout == dataTimeoutPrevious) { - connectionLost = true; - } else { - setTimeout(() => { - window.socket.close(1000, 'Normal closure'); // Force reconnection to unfreeze browser UI - }, 8000); // Timeout must be higher than TIMEOUT_DURATION - connectionLost = false; - requiresAudioStreamRestart = true; - console.log("Radio data restored."); - } - } } function handleWebSocketMessage(event) { if (event.data == 'KICK') { - console.log('Kick initiated.') + console.log('Kick initiated.'); setTimeout(() => { window.location.href = `/403?reason=${encodeURIComponent("You have been kicked")}`; }, 100); return; - } else if (event.data.startsWith("!")) { - sendToast('info', 'Info from server', event.data.slice(1), false, false) + } + if (event.data.startsWith("!")) { + sendToast('info', 'Info from server', event.data.slice(1), false, false); return; - } else if( event.data == "a0") { + } + if (event.data == "a0") { console.log('Too many users'); setTimeout(() => { window.location.href = `/403?reason=${encodeURIComponent("Too many users")}`; }, 100); return; } + if (event.data.startsWith("PING ")) { + const sentTime = Number(event.data.substring(5)); + const ping = Date.now() - sentTime; + + $('#current-ping').text(`Ping: ${ping}ms`); + + return; + } parsedData = JSON.parse(event.data); @@ -411,7 +352,6 @@ function handleWebSocketMessage(event) { const sum = signalData.reduce((acc, strNum) => acc + parseFloat(strNum), 0); data.push(sum / signalData.length); } -socket.onmessage = handleWebSocketMessage; const signalBuffer = [];