diff --git a/runtime/js/27_websocket.js b/runtime/js/27_websocket.js index 60428c24d5..9f86bdbed0 100644 --- a/runtime/js/27_websocket.js +++ b/runtime/js/27_websocket.js @@ -167,6 +167,7 @@ this.#bufferedAmount += ta.size; core.jsonOpAsync("op_ws_send", { rid: this.#rid, + kind: "binary", }, ta).then(() => { this.#bufferedAmount -= ta.size; }); @@ -193,6 +194,7 @@ this.#bufferedAmount += d.size; core.jsonOpAsync("op_ws_send", { rid: this.#rid, + kind: "text", text: string, }).then(() => { this.#bufferedAmount -= d.size; @@ -265,6 +267,13 @@ event.target = this; this.dispatchEvent(event); + this.#eventLoop(); + } else if (message.type === "ping") { + core.jsonOpAsync("op_ws_send", { + rid: this.#rid, + kind: "pong", + }); + this.#eventLoop(); } else if (message.type === "close") { this.#readyState = CLOSED; diff --git a/runtime/ops/websocket.rs b/runtime/ops/websocket.rs index 812844f393..b220655ae6 100644 --- a/runtime/ops/websocket.rs +++ b/runtime/ops/websocket.rs @@ -216,6 +216,7 @@ pub async fn op_ws_create( #[serde(rename_all = "camelCase")] struct SendArgs { rid: u32, + kind: String, text: Option, } @@ -226,9 +227,11 @@ pub async fn op_ws_send( ) -> Result { let args: SendArgs = serde_json::from_value(args)?; - let msg = match args.text { - Some(text) => Message::Text(text), - None => Message::Binary(bufs[0].to_vec()), + let msg = match args.kind.as_str() { + "text" => Message::Text(args.text.unwrap()), + "binary" => Message::Binary(bufs[0].to_vec()), + "pong" => Message::Pong(vec![]), + _ => unreachable!(), }; let rid = args.rid;