2
0
mirror of https://github.com/ad1217/PrinterStatus synced 2024-09-21 05:49:03 -04:00

server: Time out websocket if no history/status message recieved within 30 seconds

This commit is contained in:
Adam Goldsmith 2022-08-23 16:19:46 -04:00
parent eddad957b7
commit f39366cd82

View File

@ -7,6 +7,7 @@ import { Message, StatusMessage, SettingsMessage } from '../../types/messages';
import * as octoprint from '../../types/octoprint'; import * as octoprint from '../../types/octoprint';
const PING_TIME = 10000; const PING_TIME = 10000;
const STATUS_TIMEOUT = 30000;
type Timeout = ReturnType<typeof setTimeout>; type Timeout = ReturnType<typeof setTimeout>;
@ -58,6 +59,7 @@ export default class OctoprintConnection {
let pingSender: ReturnType<typeof setInterval>; let pingSender: ReturnType<typeof setInterval>;
let pongTimeout: Timeout; let pongTimeout: Timeout;
let statusTimeout: Timeout;
const url = new URL('/sockjs/websocket', this.address); const url = new URL('/sockjs/websocket', this.address);
url.protocol = 'ws'; url.protocol = 'ws';
@ -66,6 +68,14 @@ export default class OctoprintConnection {
.on('open', () => { .on('open', () => {
pingSender = setInterval(() => websocket.ping(), PING_TIME); pingSender = setInterval(() => websocket.ping(), PING_TIME);
pongTimeout = this.heartbeat(websocket, pongTimeout); 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}"`); console.log(`Connected to "${this.slug}"`);
websocket.send(JSON.stringify({ auth: session_key })); websocket.send(JSON.stringify({ auth: session_key }));
@ -81,6 +91,7 @@ export default class OctoprintConnection {
this.broadcast(ext_event); this.broadcast(ext_event);
if ('current' in event || 'history' in event) { if ('current' in event || 'history' in event) {
clearTimeout(statusTimeout);
this.lastStatus = ext_event; this.lastStatus = ext_event;
} }
}) })
@ -90,6 +101,7 @@ export default class OctoprintConnection {
.on('close', () => { .on('close', () => {
clearInterval(pingSender); clearInterval(pingSender);
clearTimeout(pongTimeout); clearTimeout(pongTimeout);
clearTimeout(statusTimeout);
console.log( console.log(
`Lost connection to "${this.slug}", attempting reconnection in 5 seconds` `Lost connection to "${this.slug}", attempting reconnection in 5 seconds`