From f39366cd8238f11a92db2d61c65224c14f2f4439 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 23 Aug 2022 16:19:46 -0400 Subject: [PATCH] server: Time out websocket if no history/status message recieved within 30 seconds --- server/src/OctoPrintConnection.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/server/src/OctoPrintConnection.ts b/server/src/OctoPrintConnection.ts index c6a68ab..456b521 100644 --- a/server/src/OctoPrintConnection.ts +++ b/server/src/OctoPrintConnection.ts @@ -7,6 +7,7 @@ import { Message, StatusMessage, SettingsMessage } from '../../types/messages'; import * as octoprint from '../../types/octoprint'; const PING_TIME = 10000; +const STATUS_TIMEOUT = 30000; type Timeout = ReturnType; @@ -58,6 +59,7 @@ export default class OctoprintConnection { let pingSender: ReturnType; let pongTimeout: Timeout; + let statusTimeout: Timeout; const url = new URL('/sockjs/websocket', this.address); url.protocol = 'ws'; @@ -66,6 +68,14 @@ export default class OctoprintConnection { .on('open', () => { pingSender = setInterval(() => websocket.ping(), PING_TIME); pongTimeout = this.heartbeat(websocket, pongTimeout); + statusTimeout = setTimeout(() => { + console.log( + `No status recieved in ${STATUS_TIMEOUT / 1000}s for "${ + this.slug + }", disconnecting` + ); + websocket.terminate(); + }, STATUS_TIMEOUT); console.log(`Connected to "${this.slug}"`); websocket.send(JSON.stringify({ auth: session_key })); @@ -81,6 +91,7 @@ export default class OctoprintConnection { this.broadcast(ext_event); if ('current' in event || 'history' in event) { + clearTimeout(statusTimeout); this.lastStatus = ext_event; } }) @@ -90,6 +101,7 @@ export default class OctoprintConnection { .on('close', () => { clearInterval(pingSender); clearTimeout(pongTimeout); + clearTimeout(statusTimeout); console.log( `Lost connection to "${this.slug}", attempting reconnection in 5 seconds`