mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-07-30 00:39:16 +02:00
Compare commits
3
Commits
87c54209ec
...
55f60168e2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55f60168e2
|
||
|
|
5360930267
|
||
|
|
a239d9dfad
|
@@ -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)) ||
|
||||
|
||||
+17
-76
@@ -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);
|
||||
|
||||
|
||||
+2
-2
@@ -17,9 +17,9 @@ if (!window.socket || window.socket.readyState === WebSocket.CLOSED || window.so
|
||||
reject(error);
|
||||
});
|
||||
|
||||
socket.addEventListener('close', () => {
|
||||
socket.addEventListener('close', (event) => {
|
||||
setTimeout(() => {
|
||||
console.warn('WebSocket connection closed');
|
||||
console.warn(`WebSocket connection closed (${event.code} '${event.reason}' ${event.wasClean})`);
|
||||
}, 100);
|
||||
reject(new Error('WebSocket connection closed'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user