fix: tweak re-connect
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
kieran 2024-06-18 12:47:47 +01:00
parent d8e0c935b9
commit 1a405f7796
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 13 additions and 7 deletions

View File

@ -36,7 +36,10 @@ export async function initRelayWorker() {
if (!conn) {
conn = await tryUseCacheRelay("ws://umbrel:4848");
}
if (conn) return;
if (conn) {
window.location.reload();
return;
}
} else if (Relay instanceof ConnectionCacheRelay) {
await Relay.connection.connect(true);
return;
@ -44,6 +47,9 @@ export async function initRelayWorker() {
} catch (e) {
localStorage.removeItem("cache-relay");
console.error(e);
if (cacheRelay) {
window.location.reload();
}
}
try {

View File

@ -45,6 +45,7 @@ export class Connection extends EventEmitter<ConnectionTypeEvents> implements Co
#activeRequests = new Set<string>();
#connectStarted = false;
#syncModule?: ConnectionSyncModule;
#wasUp = false;
id: string;
readonly address: string;
@ -169,6 +170,7 @@ export class Connection extends EventEmitter<ConnectionTypeEvents> implements Co
#onOpen(wasReconnect: boolean) {
this.#downCount = 0;
this.#connectStarted = false;
this.#wasUp = true;
this.#log(`Open!`);
this.#setupEphemeral();
this.emit("connected", wasReconnect);
@ -176,12 +178,12 @@ export class Connection extends EventEmitter<ConnectionTypeEvents> implements Co
}
#onClose(e: WebSocket.CloseEvent) {
// remote server closed the connection, dont re-connect
if (!this.#closing) {
// if not explicity closed or closed after, start re-connect timer
if (this.#wasUp && !this.#closing) {
this.#downCount++;
this.#reconnectTimer(e);
} else {
this.#log(`Closed!`);
this.#log(`Closed: connecting=${this.#connectStarted}, closing=${this.#closing}`);
this.#downCount = 0;
if (this.ReconnectTimer) {
clearTimeout(this.ReconnectTimer);
@ -206,9 +208,7 @@ export class Connection extends EventEmitter<ConnectionTypeEvents> implements Co
this.ReconnectTimer = undefined;
try {
this.connect();
} catch {
this.emit("disconnect", -1);
}
} catch {}
}, this.ConnectTimeout);
}