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