test
This commit is contained in:
parent
6ee12ed353
commit
7aa6a2a8fb
@ -11,6 +11,10 @@ let isMonitoring = false;
|
|||||||
let isConnecting = false;
|
let isConnecting = false;
|
||||||
let connectionTimestamp = null;
|
let connectionTimestamp = null;
|
||||||
|
|
||||||
|
// Backoff de reconnexion
|
||||||
|
let reconnectDelayMs = 5000;
|
||||||
|
const RECONNECT_DELAY_MAX_MS = 5 * 60 * 1000; // 5 min
|
||||||
|
|
||||||
const parseLogMessage = (log) => {
|
const parseLogMessage = (log) => {
|
||||||
// Format réel de log Palworld:
|
// Format réel de log Palworld:
|
||||||
// [2025-12-09 13:28:23] [CHAT] <LouisMazin> !link X2NMAY
|
// [2025-12-09 13:28:23] [CHAT] <LouisMazin> !link X2NMAY
|
||||||
@ -149,12 +153,26 @@ const checkAndManageWebSocket = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const resetReconnectBackoff = () => {
|
||||||
|
reconnectDelayMs = 5000;
|
||||||
|
};
|
||||||
|
|
||||||
|
const scheduleReconnect = () => {
|
||||||
|
if (!isMonitoring) return;
|
||||||
|
if (reconnectTimeout) clearTimeout(reconnectTimeout);
|
||||||
|
const delay = reconnectDelayMs;
|
||||||
|
reconnectDelayMs = Math.min(reconnectDelayMs * 2, RECONNECT_DELAY_MAX_MS);
|
||||||
|
console.log(`🔄 Reconnexion WebSocket dans ${Math.round(delay / 1000)}s...`);
|
||||||
|
reconnectTimeout = setTimeout(() => {
|
||||||
|
checkAndManageWebSocket();
|
||||||
|
}, delay);
|
||||||
|
};
|
||||||
|
|
||||||
const connectWebSocket = async (pterodactylToken, serverId) => {
|
const connectWebSocket = async (pterodactylToken, serverId) => {
|
||||||
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
|
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
|
||||||
console.log('⚠️ WebSocket déjà connecté ou en cours de connexion');
|
console.log('⚠️ WebSocket déjà connecté ou en cours de connexion');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isConnecting) {
|
if (isConnecting) {
|
||||||
console.log('⚠️ Connexion WebSocket déjà en cours');
|
console.log('⚠️ Connexion WebSocket déjà en cours');
|
||||||
return;
|
return;
|
||||||
@ -165,6 +183,7 @@ const connectWebSocket = async (pterodactylToken, serverId) => {
|
|||||||
console.log(`📅 Timestamp de connexion: ${new Date(connectionTimestamp).toISOString()}`);
|
console.log(`📅 Timestamp de connexion: ${new Date(connectionTimestamp).toISOString()}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Toujours rafraîchir les credentials pour éviter les tokens expirés après redémarrage serveur
|
||||||
const credentials = await getWebSocketCredentials(pterodactylToken, serverId);
|
const credentials = await getWebSocketCredentials(pterodactylToken, serverId);
|
||||||
|
|
||||||
ws = new WebSocket(credentials.socket, {
|
ws = new WebSocket(credentials.socket, {
|
||||||
@ -174,6 +193,7 @@ const connectWebSocket = async (pterodactylToken, serverId) => {
|
|||||||
ws.on('open', () => {
|
ws.on('open', () => {
|
||||||
console.log('✅ WebSocket Pterodactyl connecté');
|
console.log('✅ WebSocket Pterodactyl connecté');
|
||||||
isConnecting = false;
|
isConnecting = false;
|
||||||
|
resetReconnectBackoff();
|
||||||
|
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
event: 'auth',
|
event: 'auth',
|
||||||
@ -196,10 +216,7 @@ const connectWebSocket = async (pterodactylToken, serverId) => {
|
|||||||
if (heartbeatInterval) clearInterval(heartbeatInterval);
|
if (heartbeatInterval) clearInterval(heartbeatInterval);
|
||||||
heartbeatInterval = setInterval(() => {
|
heartbeatInterval = setInterval(() => {
|
||||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({ event: 'send heartbeat', args: [] }));
|
||||||
event: 'send heartbeat',
|
|
||||||
args: []
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}, 30000);
|
}, 30000);
|
||||||
}
|
}
|
||||||
@ -270,19 +287,16 @@ const connectWebSocket = async (pterodactylToken, serverId) => {
|
|||||||
heartbeatInterval = null;
|
heartbeatInterval = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tenter une reconnexion après 5 secondes
|
// Planifier une reconnexion avec backoff (utile lors des redémarrages quotidiens du serveur)
|
||||||
if (isMonitoring) {
|
scheduleReconnect();
|
||||||
console.log('🔄 Reconnexion dans 5 secondes...');
|
|
||||||
reconnectTimeout = setTimeout(() => {
|
|
||||||
checkAndManageWebSocket();
|
|
||||||
}, 5000);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erreur lors de la connexion WebSocket:', error);
|
console.error('Erreur lors de la connexion WebSocket:', error);
|
||||||
isConnecting = false;
|
isConnecting = false;
|
||||||
connectionTimestamp = null;
|
connectionTimestamp = null;
|
||||||
|
// Échec immédiat -> planifier reconnexion avec backoff
|
||||||
|
scheduleReconnect();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -308,6 +322,7 @@ const startConsoleMonitoring = (discordClient, pterodactylToken) => {
|
|||||||
|
|
||||||
client = discordClient;
|
client = discordClient;
|
||||||
isMonitoring = true;
|
isMonitoring = true;
|
||||||
|
resetReconnectBackoff();
|
||||||
|
|
||||||
const serverId = process.env.PTERODACTYL_SERVER_ID;
|
const serverId = process.env.PTERODACTYL_SERVER_ID;
|
||||||
if (!serverId) {
|
if (!serverId) {
|
||||||
@ -320,7 +335,8 @@ const startConsoleMonitoring = (discordClient, pterodactylToken) => {
|
|||||||
// Vérifier immédiatement
|
// Vérifier immédiatement
|
||||||
checkAndManageWebSocket();
|
checkAndManageWebSocket();
|
||||||
|
|
||||||
// Vérifier toutes les 30 secondes
|
// Vérifier toutes les 30 secondes (nettoyage codes et reconnect si besoin)
|
||||||
|
if (checkInterval) clearInterval(checkInterval);
|
||||||
checkInterval = setInterval(checkAndManageWebSocket, 30000);
|
checkInterval = setInterval(checkAndManageWebSocket, 30000);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user