mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Fix halfopen websocket (#18934)
* initial suggestion * Refactored implementation * Updated implementation * Cancel existing inverval
This commit is contained in:
parent
1faa1480e4
commit
677cffd650
@ -33,11 +33,14 @@ import { fetchWithAuth } from "../util/fetch-with-auth";
|
|||||||
import { getState } from "../util/ha-pref-storage";
|
import { getState } from "../util/ha-pref-storage";
|
||||||
import hassCallApi from "../util/hass-call-api";
|
import hassCallApi from "../util/hass-call-api";
|
||||||
import { HassBaseEl } from "./hass-base-mixin";
|
import { HassBaseEl } from "./hass-base-mixin";
|
||||||
|
import { promiseTimeout } from "../common/util/promise-timeout";
|
||||||
|
|
||||||
export const connectionMixin = <T extends Constructor<HassBaseEl>>(
|
export const connectionMixin = <T extends Constructor<HassBaseEl>>(
|
||||||
superClass: T
|
superClass: T
|
||||||
) =>
|
) =>
|
||||||
class extends superClass {
|
class extends superClass {
|
||||||
|
private __backendPingInterval?: ReturnType<typeof setInterval>;
|
||||||
|
|
||||||
protected initializeHass(auth: Auth, conn: Connection) {
|
protected initializeHass(auth: Auth, conn: Connection) {
|
||||||
const language = getLocalLanguage();
|
const language = getLocalLanguage();
|
||||||
|
|
||||||
@ -269,6 +272,21 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
|
|||||||
subscribeFrontendUserData(conn, "core", (userData) =>
|
subscribeFrontendUserData(conn, "core", (userData) =>
|
||||||
this._updateHass({ userData })
|
this._updateHass({ userData })
|
||||||
);
|
);
|
||||||
|
|
||||||
|
clearInterval(this.__backendPingInterval);
|
||||||
|
this.__backendPingInterval = setInterval(() => {
|
||||||
|
if (this.hass?.connected) {
|
||||||
|
promiseTimeout(5000, this.hass?.connection.ping()).catch(() => {
|
||||||
|
if (!this.hass?.connected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log("Websocket died, forcing reconnect...");
|
||||||
|
this.hass?.connection.reconnect(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected hassReconnected() {
|
protected hassReconnected() {
|
||||||
@ -293,5 +311,6 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
|
|||||||
super.hassDisconnected();
|
super.hassDisconnected();
|
||||||
this._updateHass({ connected: false });
|
this._updateHass({ connected: false });
|
||||||
broadcastConnectionStatus("disconnected");
|
broadcastConnectionStatus("disconnected");
|
||||||
|
clearInterval(this.__backendPingInterval);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user