This commit is contained in:
Shivering Ocelot committed 2026-09-12 14:31:47 -05:00
1 parent 953976cfc1
commit 62f5fa87a9
1 file changed
+24
+24
View File
@@ -2,6 +2,8 @@
// Modeled on the browser client in reference/backend.js: nonce-tracked actions, // Modeled on the browser client in reference/backend.js: nonce-tracked actions,
// action listeners, orphan handlers for the auth response, auto-reconnect. // action listeners, orphan handlers for the auth response, auto-reconnect.
//import WebSocket from 'ws';
export class Horde { export class Horde {
constructor({ host, token = null }) { constructor({ host, token = null }) {
this.host = host; this.host = host;
@@ -14,6 +16,7 @@ export class Horde {
this.connectListeners = []; this.connectListeners = [];
this.reconnectTimeout = null; this.reconnectTimeout = null;
this.stopped = false; this.stopped = false;
this.pingTimeout = null; // Tracks the heartbeat interval
} }
connect() { connect() {
@@ -44,6 +47,9 @@ export class Horde {
console.error('Error during hub login:', err); console.error('Error during hub login:', err);
socket.close(); socket.close();
} }
// Start heartbeat
this.schedulePing();
}; };
socket.onmessage = (event) => { socket.onmessage = (event) => {
@@ -57,6 +63,7 @@ export class Horde {
console.log('Disconnected from hub. Reconnecting in 1s...'); console.log('Disconnected from hub. Reconnecting in 1s...');
this.reconnectTimeout = setTimeout(() => this.connect(), 1000); this.reconnectTimeout = setTimeout(() => this.connect(), 1000);
} }
this.pingTimeout = null;
}; };
socket.onerror = (event) => { socket.onerror = (event) => {
@@ -67,6 +74,7 @@ export class Horde {
close() { close() {
this.stopped = true; this.stopped = true;
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout); if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
if (this.pingTimeout) clearTimeout(this.pingTimeout);
if (this.socket) { if (this.socket) {
this.socket.onclose = null; this.socket.onclose = null;
this.socket.close(); this.socket.close();
@@ -160,4 +168,20 @@ export class Horde {
if (packet.error) console.error('Hub error:', packet.error); if (packet.error) console.error('Hub error:', packet.error);
else console.warn('Ignored hub message:', packet.action); else console.warn('Ignored hub message:', packet.action);
} }
schedulePing() {
if (this.pingTimeout) clearTimeout(this.pingTimeout);
this.pingTimeout = setTimeout(() => {
if (this.stopped) {
this.pingTimeout = null;
return;
}
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.sendAction("ping");
this.schedulePing();
} else {
this.pingTimeout = null;
}
}, 30_000);
}
} }