diff --git a/lib/agent/plugins/control-panel/websockets/index.js b/lib/agent/plugins/control-panel/websockets/index.js index 60a5d6893..10a5798a9 100644 --- a/lib/agent/plugins/control-panel/websockets/index.js +++ b/lib/agent/plugins/control-panel/websockets/index.js @@ -22,6 +22,7 @@ let hooks; let config; let ws; let pingTimeout; +let pingPongTimeout; let emitter; let setAliveTimeInterval = null; let setIntervalWSStatus = null; @@ -31,6 +32,7 @@ let idTimeoutToCancel; let websocketConnected = false; let startupTimeout = 5000; +let heartbeatTimeout = 120000 + 1000; exports.re_schedule = true; exports.responses_queue = []; @@ -82,14 +84,14 @@ const updateStoredConnection = (newStoredTime) => { }) } exports.heartbeat = () => { - if (!ws || !ws.readyState || ws.readyState !== 1) exports.heartbeatTimed(15000); + if (!ws || !ws.readyState || ws.readyState !== 1) restartWebsocketCall(); }; -exports.heartbeatTimed = (timeToRetry) => { +exports.heartbeatTimed = () => { if (pingTimeout) clearTimeout(pingTimeout); pingTimeout = setTimeout(() => { restartWebsocketCall(); - }, timeToRetry); + }, heartbeatTimeout); }; const restartWebsocketCall = () => { @@ -180,6 +182,7 @@ exports.startWebsocket = () => { ws = new WebSocket(`${protocol}://${url}`, options); websocketConnected = true; ws.on('open', () => { + pingInterval = setInterval(() => { ws.ping() }, 60000); exports.notify_status(status); storage.do('all', { type: 'responses' }, (errs, actions) => { if (!actions || typeof actions === 'undefined') return; @@ -250,6 +253,7 @@ exports.startWebsocket = () => { }); ws.on('ping', () => { + heartbeatTimed(); if (!ws || !ws.readyState || ws.readyState !== 1) return; ws.pong(); }); @@ -374,6 +378,8 @@ const clearIntervals = (aliveTimeReset = false) => { if(notifyActionInterval) clearInterval(notifyActionInterval); if(getStatusInterval) clearInterval(getStatusInterval); if(setIntervalWSStatus) clearInterval(setIntervalWSStatus); + if(pingTimeout) clearInterval(pingTimeout); + if(pingInterval) clearIntervals(pingInterval); if(setAliveTimeInterval && aliveTimeReset) clearInterval(setAliveTimeInterval); }