From 2b5cc6b49846c89b3e50a6f51688d527520e4cc4 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 23 Apr 2021 01:31:34 +0200 Subject: [PATCH] fix: parse websocket messages correctly (#10318) --- op_crates/websocket/01_websocket.js | 101 +++++++++++++++------------- op_crates/websocket/lib.rs | 2 +- 2 files changed, 57 insertions(+), 46 deletions(-) diff --git a/op_crates/websocket/01_websocket.js b/op_crates/websocket/01_websocket.js index a37cd01cca..374d174b02 100644 --- a/op_crates/websocket/01_websocket.js +++ b/op_crates/websocket/01_websocket.js @@ -307,60 +307,71 @@ async #eventLoop() { while (this.#readyState === OPEN) { - const message = await core.opAsync( + const { kind, value } = await core.opAsync( "op_ws_next_event", this.#rid, ); - if ("string" in message) { - const event = new MessageEvent("message", { - data: message.string, - origin: this.#url, - }); - event.target = this; - this.dispatchEvent(event); - } else if ("binary" in message) { - let data; - - if (this.binaryType === "blob") { - data = new Blob([new Uint8Array(message.binary)]); - } else { - data = new Uint8Array(message.binary).buffer; + switch (kind) { + case "string": { + const event = new MessageEvent("message", { + data: value, + origin: this.#url, + }); + event.target = this; + this.dispatchEvent(event); + break; } + case "binary": { + let data; - const event = new MessageEvent("message", { - data, - origin: this.#url, - }); - event.target = this; - this.dispatchEvent(event); - } else if ("ping" in message) { - core.opAsync("op_ws_send", { - rid: this.#rid, - kind: "pong", - }); - } else if ("close" in message) { - this.#readyState = CLOSED; + if (this.binaryType === "blob") { + data = new Blob([new Uint8Array(value)]); + } else { + data = new Uint8Array(value).buffer; + } - const event = new CloseEvent("close", { - wasClean: true, - code: message.close.code, - reason: message.close.reason, - }); - event.target = this; - this.dispatchEvent(event); - tryClose(this.#rid); - } else if ("error" in message) { - this.#readyState = CLOSED; + const event = new MessageEvent("message", { + data, + origin: this.#url, + }); + event.target = this; + this.dispatchEvent(event); + break; + } + case "ping": { + core.opAsync("op_ws_send", { + rid: this.#rid, + kind: "pong", + }); + break; + } + case "close": { + this.#readyState = CLOSED; - const errorEv = new ErrorEvent("error"); - errorEv.target = this; - this.dispatchEvent(errorEv); + const event = new CloseEvent("close", { + wasClean: true, + code: value.code, + reason: value.reason, + }); + event.target = this; + this.dispatchEvent(event); + tryClose(this.#rid); + break; + } + case "error": { + this.#readyState = CLOSED; - const closeEv = new CloseEvent("close"); - closeEv.target = this; - this.dispatchEvent(closeEv); - tryClose(this.#rid); + const errorEv = new ErrorEvent("error"); + errorEv.target = this; + this.dispatchEvent(errorEv); + + const closeEv = new CloseEvent("close"); + closeEv.target = this; + this.dispatchEvent(closeEv); + tryClose(this.#rid); + break; + } } } } diff --git a/op_crates/websocket/lib.rs b/op_crates/websocket/lib.rs index c7567c1970..daf61a9083 100644 --- a/op_crates/websocket/lib.rs +++ b/op_crates/websocket/lib.rs @@ -282,7 +282,7 @@ pub async fn op_ws_close( } #[derive(Serialize)] -#[serde(rename_all = "camelCase")] +#[serde(tag = "kind", content = "value", rename_all = "camelCase")] pub enum NextEventResponse { String(String), Binary(Vec),