diff --git a/apps/web/src/lib/platform-ws.ts b/apps/web/src/lib/platform-ws.ts index afff38b..1431911 100644 --- a/apps/web/src/lib/platform-ws.ts +++ b/apps/web/src/lib/platform-ws.ts @@ -59,22 +59,49 @@ class NativeSocketAdapter implements PlatformSocket { } } +type NativeMsg = { type: string; data: unknown }; + class TauriSocketAdapter implements PlatformSocket { onopen: (() => void) | null = null; onmessage: ((ev: PlatformMessage) => void) | null = null; onclose: (() => void) | null = null; onerror: (() => void) | null = null; #closed = false; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - #conn: any = null; + #id: number | null = null; + // Frames that arrive before connect() resolves, delivered right after onopen. + #early: NativeMsg[] | null = []; constructor(url: string) { void this.#connect(url); } + #deliver(msg: NativeMsg): void { + if (this.#closed) return; + if (this.#early) { + this.#early.push(msg); + return; + } + if (msg.type === "Text") { + this.onmessage?.({ data: msg.data as string }); + } else if (msg.type === "Binary") { + // Camera live view (routes/cameras.ts): one JPEG per binary message. The + // plugin hands bytes over IPC as a number array. + this.onmessage?.({ data: new Uint8Array(msg.data as number[]).buffer }); + } else if (msg.type === "Close") { + this.onclose?.(); + } + // Ping/Pong: nothing to do. + } + async #connect(url: string): Promise { try { - const { default: TauriWebSocket } = await import("@tauri-apps/plugin-websocket"); + // The plugin's own JS wrapper (@tauri-apps/plugin-websocket) only lets a listener + // be added AFTER connect() resolves, but the native side starts reading at the + // handshake — so a frame the server sends at once (the live feed's `hello`, with + // the full device list) could reach an empty listener set and be lost: the desktop + // footer then showed only the devices that happened to push later (2026-11-19). + // So call the plugin's commands directly, with our channel listening from the start. + const { Channel, invoke } = await import("@tauri-apps/api/core"); if (this.#closed) return; // close() called before connect resolved // The native WS plugin is a bare tungstenite client: no page context AND // no cookie jar. Two consequences, both handled via explicit headers: @@ -88,27 +115,22 @@ class TauriSocketAdapter implements PlatformSocket { // x-ws-ticket header (see routes/ws.ts). const ticket = await fetchWsTicket(); if (this.#closed) return; - const conn = await TauriWebSocket.connect(url, { - headers: { Origin: "tauri://localhost", "x-ws-ticket": ticket }, + const onMessage = new Channel(); + onMessage.onmessage = (msg) => this.#deliver(msg); + const id = await invoke("plugin:websocket|connect", { + url, + onMessage, + config: { headers: [["Origin", "tauri://localhost"], ["x-ws-ticket", ticket]] }, }); + this.#id = id; if (this.#closed) { - void conn.disconnect(); + this.#disconnect(); return; } - this.#conn = conn; - conn.addListener((msg: { type: string; data: unknown }) => { - if (msg.type === "Text") { - this.onmessage?.({ data: msg.data as string }); - } else if (msg.type === "Binary") { - // Camera live view (routes/cameras.ts): one JPEG per binary message. The - // plugin hands bytes over IPC as a number array. - this.onmessage?.({ data: new Uint8Array(msg.data as number[]).buffer }); - } else if (msg.type === "Close") { - this.onclose?.(); - } - // Ping/Pong: nothing to do. - }); this.onopen?.(); + const early = this.#early ?? []; + this.#early = null; + for (const msg of early) this.#deliver(msg); } catch (err) { // logClient, not console.error: console output only reaches app_logs at // debug/trace level, which is how the ticket-less 401 stayed invisible @@ -126,9 +148,23 @@ class TauriSocketAdapter implements PlatformSocket { } } + #disconnect(): void { + if (this.#id == null) return; + void import("@tauri-apps/api/core") + .then(({ invoke }) => + invoke("plugin:websocket|send", { + id: this.#id, + message: { type: "Close", data: { code: 1000, reason: "Disconnected by client" } }, + }), + ) + .catch(() => { + /* already gone */ + }); + } + close(): void { this.#closed = true; - void this.#conn?.disconnect(); + this.#disconnect(); } }