This commit is contained in:
Mateusz Gruszczyński
2026-09-17 13:11:37 +02:00
parent 86a6fa1f2f
commit eede4e88e6
10 changed files with 145 additions and 20 deletions
+21 -3
View File
@@ -7,6 +7,7 @@
* See LICENSE file in repository root for details.
*/
import { reconnectStatus } from "@rustpad/connection-state";
import { logError, logInfo, logWarn } from "@rustpad/logger";
const DEFAULT_HEARTBEAT_INTERVAL_MS = 10000;
@@ -68,8 +69,12 @@ class RoomSocket {
}
this.intentionalClose = false;
this.onStatus?.(this.reconnectAttempt ? "reconnecting" : "connecting", {
const status = this.reconnectAttempt
? reconnectStatus(this.reconnectAttempt, navigator.onLine)
: "connecting";
this.onStatus?.(status, {
attempt: this.reconnectAttempt,
persistent: status === "error",
message: this.reconnectAttempt ? "Re-establishing the live connection." : "Opening the live connection.",
});
this.emitDiagnostics();
@@ -122,6 +127,7 @@ class RoomSocket {
}
if (message.type === "error") {
this.intentionalClose = true;
this.onStatus?.("error", { message: message.message, fatal: true });
this.onError?.(message.message);
socket.close();
return;
@@ -204,7 +210,13 @@ class RoomSocket {
const baseDelay = Math.min(this.maxReconnectDelayMs, 750 * (2 ** Math.min(this.reconnectAttempt - 1, 4)));
const retryInMs = navigator.onLine ? baseDelay : 3000;
this.totalReconnects += 1;
this.onStatus?.("reconnecting", { attempt: this.reconnectAttempt, message, retryInMs });
const status = reconnectStatus(this.reconnectAttempt, navigator.onLine);
this.onStatus?.(status, {
attempt: this.reconnectAttempt,
persistent: status === "error",
message,
retryInMs,
});
this.emitDiagnostics();
this.reconnectTimer = window.setTimeout(() => this.connect(), retryInMs);
}
@@ -212,7 +224,13 @@ class RoomSocket {
reconnectNow(message) {
if (this.stopped || this.socket?.readyState === WebSocket.OPEN || this.socket?.readyState === WebSocket.CONNECTING) return;
clearTimeout(this.reconnectTimer);
this.onStatus?.("reconnecting", { attempt: this.reconnectAttempt, message, retryInMs: 0 });
const status = reconnectStatus(this.reconnectAttempt, navigator.onLine);
this.onStatus?.(status, {
attempt: this.reconnectAttempt,
persistent: status === "error",
message,
retryInMs: 0,
});
this.connect();
}