From 2f5aec28831d2333841d635a012e96dbfaef8fda Mon Sep 17 00:00:00 2001 From: leonardmgh Date: Mon, 4 Dec 2023 21:12:24 +0000 Subject: [PATCH 1/4] initial suggestion --- src/state/connection-mixin.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index d4ce2419ed6c..4addcc7d865e 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -38,6 +38,8 @@ export const connectionMixin = >( superClass: T ) => class extends superClass { + pingInterval: ReturnType | undefined; + protected initializeHass(auth: Auth, conn: Connection) { const language = getLocalLanguage(); @@ -269,6 +271,30 @@ export const connectionMixin = >( subscribeFrontendUserData(conn, "core", (userData) => this._updateHass({ userData }) ); + + if (this.pingInterval !== undefined) { + clearInterval(this.pingInterval); + } + + this.pingInterval = setInterval(async () => { + // eslint-disable-next-line no-console + console.log("This function is called periodically."); + + if (!this.hass!.connected) { + return; + } + + // Force reconnection since socket in some cases can be closed gracefully + const timeout = setTimeout(() => { + if (!this.hass?.connected) { + return; + } + this.hass?.connection.reconnect(true); + }, 5000); + + await this.hass?.connection.ping(); + clearTimeout(timeout); + }, 20000); } protected hassReconnected() { From a18d1425587813b2ab36eb3a76eb031924569919 Mon Sep 17 00:00:00 2001 From: leonardmgh Date: Wed, 6 Dec 2023 21:18:06 +0000 Subject: [PATCH 2/4] Refactored implementation --- src/state/connection-mixin.ts | 36 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index 4addcc7d865e..6cd15afd3a7f 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -33,13 +33,12 @@ import { fetchWithAuth } from "../util/fetch-with-auth"; import { getState } from "../util/ha-pref-storage"; import hassCallApi from "../util/hass-call-api"; import { HassBaseEl } from "./hass-base-mixin"; +import { promiseTimeout } from "../common/util/promise-timeout"; export const connectionMixin = >( superClass: T ) => class extends superClass { - pingInterval: ReturnType | undefined; - protected initializeHass(auth: Auth, conn: Connection) { const language = getLocalLanguage(); @@ -272,29 +271,22 @@ export const connectionMixin = >( this._updateHass({ userData }) ); - if (this.pingInterval !== undefined) { - clearInterval(this.pingInterval); - } - - this.pingInterval = setInterval(async () => { - // eslint-disable-next-line no-console - console.log("This function is called periodically."); + const pingBackend = async () => { + if (this.hass?.connected) { + promiseTimeout(5000, this.hass?.connection.ping()).catch(() => { + if (!this.hass?.connected) { + return; + } - if (!this.hass!.connected) { - return; + // eslint-disable-next-line no-console + console.log("Websocket died, forcing reconnect..."); + this.hass?.connection.reconnect(true); + }); } + setTimeout(pingBackend, 5000); + }; - // Force reconnection since socket in some cases can be closed gracefully - const timeout = setTimeout(() => { - if (!this.hass?.connected) { - return; - } - this.hass?.connection.reconnect(true); - }, 5000); - - await this.hass?.connection.ping(); - clearTimeout(timeout); - }, 20000); + pingBackend(); } protected hassReconnected() { From 340fa06d257bb67a3bc0cc5d0ff12019b8da004a Mon Sep 17 00:00:00 2001 From: Leonard Hansen Date: Mon, 15 Jul 2024 15:15:46 +0200 Subject: [PATCH 3/4] Updated implementation --- src/state/connection-mixin.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index 6cd15afd3a7f..ce94324cebd4 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -39,6 +39,8 @@ export const connectionMixin = >( superClass: T ) => class extends superClass { + private __backendPingInterval?: ReturnType; + protected initializeHass(auth: Auth, conn: Connection) { const language = getLocalLanguage(); @@ -271,7 +273,7 @@ export const connectionMixin = >( this._updateHass({ userData }) ); - const pingBackend = async () => { + this.__backendPingInterval = setInterval(() => { if (this.hass?.connected) { promiseTimeout(5000, this.hass?.connection.ping()).catch(() => { if (!this.hass?.connected) { @@ -283,10 +285,7 @@ export const connectionMixin = >( this.hass?.connection.reconnect(true); }); } - setTimeout(pingBackend, 5000); - }; - - pingBackend(); + }, 10000); } protected hassReconnected() { @@ -311,5 +310,6 @@ export const connectionMixin = >( super.hassDisconnected(); this._updateHass({ connected: false }); broadcastConnectionStatus("disconnected"); + clearInterval(this.__backendPingInterval); } }; From 2f481ca6d1d657e3e388f984753e5be192fff6e1 Mon Sep 17 00:00:00 2001 From: Leonard Hansen Date: Tue, 16 Jul 2024 13:35:43 +0200 Subject: [PATCH 4/4] Cancel existing inverval --- src/state/connection-mixin.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index ce94324cebd4..1a429239ef66 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -273,6 +273,7 @@ export const connectionMixin = >( this._updateHass({ userData }) ); + clearInterval(this.__backendPingInterval); this.__backendPingInterval = setInterval(() => { if (this.hass?.connected) { promiseTimeout(5000, this.hass?.connection.ping()).catch(() => {