From 6804b2f8891822f213f2e2a417f1659d2df77e02 Mon Sep 17 00:00:00 2001 From: crowlKats <13135287+crowlKats@users.noreply.github.com> Date: Fri, 30 Apr 2021 17:03:50 +0200 Subject: refactor(websocket): use ZeroCopyBuf to return binary data (#10446) --- op_crates/websocket/01_websocket.js | 4 ++-- op_crates/websocket/lib.rs | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/op_crates/websocket/01_websocket.js b/op_crates/websocket/01_websocket.js index 374d174b0..c77af4566 100644 --- a/op_crates/websocket/01_websocket.js +++ b/op_crates/websocket/01_websocket.js @@ -326,9 +326,9 @@ let data; if (this.binaryType === "blob") { - data = new Blob([new Uint8Array(value)]); + data = new Blob([value]); } else { - data = new Uint8Array(value).buffer; + data = value.buffer; } const event = new MessageEvent("message", { diff --git a/op_crates/websocket/lib.rs b/op_crates/websocket/lib.rs index b81b1701d..acf823775 100644 --- a/op_crates/websocket/lib.rs +++ b/op_crates/websocket/lib.rs @@ -289,7 +289,7 @@ pub async fn op_ws_close( #[serde(tag = "kind", content = "value", rename_all = "camelCase")] pub enum NextEventResponse { String(String), - Binary(Vec), + Binary(ZeroCopyBuf), Close { code: u16, reason: String }, Ping, Pong, @@ -313,10 +313,7 @@ pub async fn op_ws_next_event( let val = rx.next().or_cancel(cancel).await?; let res = match val { Some(Ok(Message::Text(text))) => NextEventResponse::String(text), - Some(Ok(Message::Binary(data))) => { - // TODO(ry): don't use json to send binary data. - NextEventResponse::Binary(data) - } + Some(Ok(Message::Binary(data))) => NextEventResponse::Binary(data.into()), Some(Ok(Message::Close(Some(frame)))) => NextEventResponse::Close { code: frame.code.into(), reason: frame.reason.to_string(), -- cgit v1.2.3