This commit is contained in:
Mateusz Gruszczyński
2026-08-01 09:41:10 +02:00
parent 50adb291d6
commit 7006b4ea09
11 changed files with 42 additions and 143 deletions
-18
View File
@@ -30,7 +30,6 @@ class RoomSocket {
this.latencySampleWindow = DEFAULT_LATENCY_SAMPLE_WINDOW;
this.latencySamples = [];
this.totalReconnects = 0;
this.connectedAt = null;
this.authenticatedAt = null;
this.lastConnectionUptimeMs = 0;
this.lastMessageAt = null;
@@ -51,15 +50,10 @@ class RoomSocket {
get kind() { return "room"; }
clientDiagnostics() {
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
return {
language: navigator.language || null,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || null,
platform: navigator.userAgentData?.platform || navigator.platform || null,
effective_type: connection?.effectiveType || null,
downlink_mbps: Number.isFinite(connection?.downlink) ? connection.downlink : null,
network_rtt_ms: Number.isFinite(connection?.rtt) ? Math.max(0, Math.round(connection.rtt)) : null,
save_data: typeof connection?.saveData === "boolean" ? connection.saveData : null,
};
}
@@ -93,7 +87,6 @@ class RoomSocket {
socket.addEventListener("open", () => {
if (socket !== this.socket || this.stopped) return;
logInfo("websocket.open", { kind: this.kind });
this.connectedAt = Date.now();
this.serverDiagnostics = null;
this.lastClose = null;
this.send({
@@ -161,7 +154,6 @@ class RoomSocket {
this.onLatency?.(null);
const closedAt = Date.now();
this.lastConnectionUptimeMs = this.authenticatedAt ? Math.max(0, closedAt - this.authenticatedAt) : this.lastConnectionUptimeMs;
this.connectedAt = null;
this.authenticatedAt = null;
this.lastClose = { code: event.code, reason: event.reason || "", at: closedAt };
this.emitDiagnostics();
@@ -260,7 +252,6 @@ class RoomSocket {
this.latencySampleWindow = Math.round(positiveNumber(diagnostics.latency_sample_window, this.latencySampleWindow, 3, 100));
while (this.latencySamples.length > this.latencySampleWindow) this.latencySamples.shift();
if (this.socket?.readyState === WebSocket.OPEN && this.authenticatedAt) this.startPing();
this.onServerDiagnostics?.(diagnostics);
this.emitDiagnostics();
}
@@ -282,31 +273,22 @@ class RoomSocket {
}
emitDiagnostics() {
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
const readyStates = ["connecting", "open", "closing", "closed"];
this.onDiagnostics?.({
server: this.serverDiagnostics,
runtime: {
state: readyStates[this.socket?.readyState ?? WebSocket.CLOSED] || "closed",
online: navigator.onLine,
visibility: document.visibilityState,
uptime_ms: this.authenticatedAt ? Math.max(0, Date.now() - this.authenticatedAt) : 0,
last_connection_uptime_ms: this.lastConnectionUptimeMs,
reconnect_attempt: this.reconnectAttempt,
total_reconnects: this.totalReconnects,
connected_at: this.connectedAt,
authenticated_at: this.authenticatedAt,
last_message_at: this.lastMessageAt,
last_close: this.lastClose,
buffered_amount: this.socket?.bufferedAmount || 0,
bytes_sent: this.bytesSent,
bytes_received: this.bytesReceived,
network: {
effective_type: connection?.effectiveType || null,
downlink_mbps: Number.isFinite(connection?.downlink) ? connection.downlink : null,
rtt_ms: Number.isFinite(connection?.rtt) ? connection.rtt : null,
save_data: typeof connection?.saveData === "boolean" ? connection.saveData : null,
},
latency: this.latencyStats(),
},
});