Skip to content

Commit

Permalink
fix(runtime/websocket): respond to ping with pong (denoland#8974)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Jan 5, 2021
1 parent c823211 commit f85cd54
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions runtime/js/27_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
this.#bufferedAmount += ta.size;
core.jsonOpAsync("op_ws_send", {
rid: this.#rid,
kind: "binary",
}, ta).then(() => {
this.#bufferedAmount -= ta.size;
});
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions runtime/ops/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub async fn op_ws_create(
#[serde(rename_all = "camelCase")]
struct SendArgs {
rid: u32,
kind: String,
text: Option<String>,
}

Expand All @@ -226,9 +227,11 @@ pub async fn op_ws_send(
) -> Result<Value, AnyError> {
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;

Expand Down

0 comments on commit f85cd54

Please sign in to comment.