1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -05:00

fix(runtime/websocket): respond to ping with pong (#8974)

This commit is contained in:
crowlKats 2021-01-05 13:37:02 +01:00 committed by GitHub
parent c823211a2c
commit f85cd54cb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View file

@ -167,6 +167,7 @@
this.#bufferedAmount += ta.size; this.#bufferedAmount += ta.size;
core.jsonOpAsync("op_ws_send", { core.jsonOpAsync("op_ws_send", {
rid: this.#rid, rid: this.#rid,
kind: "binary",
}, ta).then(() => { }, ta).then(() => {
this.#bufferedAmount -= ta.size; this.#bufferedAmount -= ta.size;
}); });
@ -193,6 +194,7 @@
this.#bufferedAmount += d.size; this.#bufferedAmount += d.size;
core.jsonOpAsync("op_ws_send", { core.jsonOpAsync("op_ws_send", {
rid: this.#rid, rid: this.#rid,
kind: "text",
text: string, text: string,
}).then(() => { }).then(() => {
this.#bufferedAmount -= d.size; this.#bufferedAmount -= d.size;
@ -265,6 +267,13 @@
event.target = this; event.target = this;
this.dispatchEvent(event); this.dispatchEvent(event);
this.#eventLoop();
} else if (message.type === "ping") {
core.jsonOpAsync("op_ws_send", {
rid: this.#rid,
kind: "pong",
});
this.#eventLoop(); this.#eventLoop();
} else if (message.type === "close") { } else if (message.type === "close") {
this.#readyState = CLOSED; this.#readyState = CLOSED;

View file

@ -216,6 +216,7 @@ pub async fn op_ws_create(
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct SendArgs { struct SendArgs {
rid: u32, rid: u32,
kind: String,
text: Option<String>, text: Option<String>,
} }
@ -226,9 +227,11 @@ pub async fn op_ws_send(
) -> Result<Value, AnyError> { ) -> Result<Value, AnyError> {
let args: SendArgs = serde_json::from_value(args)?; let args: SendArgs = serde_json::from_value(args)?;
let msg = match args.text { let msg = match args.kind.as_str() {
Some(text) => Message::Text(text), "text" => Message::Text(args.text.unwrap()),
None => Message::Binary(bufs[0].to_vec()), "binary" => Message::Binary(bufs[0].to_vec()),
"pong" => Message::Pong(vec![]),
_ => unreachable!(),
}; };
let rid = args.rid; let rid = args.rid;