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

View File

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