fix(desktop): don't lose the first WebSocket frame

tauri-plugin-websocket starts reading at the handshake but its JS wrapper
only takes a listener after connect() resolves, so the server's immediate
hello (full device list) could be dropped: the desktop footer showed only
devices that pushed later. Call the plugin's commands directly with our own
channel listening from the start; hold early frames until onopen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4
This commit is contained in:
2026-11-19 12:38:53 +01:00
parent ee0e1ff575
commit b69faec547
+56 -20
View File
@@ -59,22 +59,49 @@ class NativeSocketAdapter implements PlatformSocket {
} }
} }
type NativeMsg = { type: string; data: unknown };
class TauriSocketAdapter implements PlatformSocket { class TauriSocketAdapter implements PlatformSocket {
onopen: (() => void) | null = null; onopen: (() => void) | null = null;
onmessage: ((ev: PlatformMessage) => void) | null = null; onmessage: ((ev: PlatformMessage) => void) | null = null;
onclose: (() => void) | null = null; onclose: (() => void) | null = null;
onerror: (() => void) | null = null; onerror: (() => void) | null = null;
#closed = false; #closed = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any #id: number | null = null;
#conn: any = null; // Frames that arrive before connect() resolves, delivered right after onopen.
#early: NativeMsg[] | null = [];
constructor(url: string) { constructor(url: string) {
void this.#connect(url); 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<void> { async #connect(url: string): Promise<void> {
try { 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 if (this.#closed) return; // close() called before connect resolved
// The native WS plugin is a bare tungstenite client: no page context AND // The native WS plugin is a bare tungstenite client: no page context AND
// no cookie jar. Two consequences, both handled via explicit headers: // 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). // x-ws-ticket header (see routes/ws.ts).
const ticket = await fetchWsTicket(); const ticket = await fetchWsTicket();
if (this.#closed) return; if (this.#closed) return;
const conn = await TauriWebSocket.connect(url, { const onMessage = new Channel<NativeMsg>();
headers: { Origin: "tauri://localhost", "x-ws-ticket": ticket }, onMessage.onmessage = (msg) => this.#deliver(msg);
const id = await invoke<number>("plugin:websocket|connect", {
url,
onMessage,
config: { headers: [["Origin", "tauri://localhost"], ["x-ws-ticket", ticket]] },
}); });
this.#id = id;
if (this.#closed) { if (this.#closed) {
void conn.disconnect(); this.#disconnect();
return; 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?.(); this.onopen?.();
const early = this.#early ?? [];
this.#early = null;
for (const msg of early) this.#deliver(msg);
} catch (err) { } catch (err) {
// logClient, not console.error: console output only reaches app_logs at // logClient, not console.error: console output only reaches app_logs at
// debug/trace level, which is how the ticket-less 401 stayed invisible // 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 { close(): void {
this.#closed = true; this.#closed = true;
void this.#conn?.disconnect(); this.#disconnect();
} }
} }