Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix halfopen websocket #18934

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/state/connection-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ 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 = <T extends Constructor<HassBaseEl>>(
superClass: T
) =>
class extends superClass {
private __backendPingInterval?: ReturnType<typeof setInterval>;

protected initializeHass(auth: Auth, conn: Connection) {
const language = getLocalLanguage();

Expand Down Expand Up @@ -269,6 +272,20 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
subscribeFrontendUserData(conn, "core", (userData) =>
this._updateHass({ userData })
);

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);
leonardmgh marked this conversation as resolved.
Show resolved Hide resolved
}

protected hassReconnected() {
Expand All @@ -293,5 +310,6 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
super.hassDisconnected();
this._updateHass({ connected: false });
broadcastConnectionStatus("disconnected");
clearInterval(this.__backendPingInterval);
}
};