mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 23:34:47 -05:00
fix: parse websocket messages correctly (#10318)
This commit is contained in:
parent
21ab4d94c0
commit
2b5cc6b498
2 changed files with 57 additions and 46 deletions
|
@ -307,25 +307,28 @@
|
|||
|
||||
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) {
|
||||
switch (kind) {
|
||||
case "string": {
|
||||
const event = new MessageEvent("message", {
|
||||
data: message.string,
|
||||
data: value,
|
||||
origin: this.#url,
|
||||
});
|
||||
event.target = this;
|
||||
this.dispatchEvent(event);
|
||||
} else if ("binary" in message) {
|
||||
break;
|
||||
}
|
||||
case "binary": {
|
||||
let data;
|
||||
|
||||
if (this.binaryType === "blob") {
|
||||
data = new Blob([new Uint8Array(message.binary)]);
|
||||
data = new Blob([new Uint8Array(value)]);
|
||||
} else {
|
||||
data = new Uint8Array(message.binary).buffer;
|
||||
data = new Uint8Array(value).buffer;
|
||||
}
|
||||
|
||||
const event = new MessageEvent("message", {
|
||||
|
@ -334,23 +337,29 @@
|
|||
});
|
||||
event.target = this;
|
||||
this.dispatchEvent(event);
|
||||
} else if ("ping" in message) {
|
||||
break;
|
||||
}
|
||||
case "ping": {
|
||||
core.opAsync("op_ws_send", {
|
||||
rid: this.#rid,
|
||||
kind: "pong",
|
||||
});
|
||||
} else if ("close" in message) {
|
||||
break;
|
||||
}
|
||||
case "close": {
|
||||
this.#readyState = CLOSED;
|
||||
|
||||
const event = new CloseEvent("close", {
|
||||
wasClean: true,
|
||||
code: message.close.code,
|
||||
reason: message.close.reason,
|
||||
code: value.code,
|
||||
reason: value.reason,
|
||||
});
|
||||
event.target = this;
|
||||
this.dispatchEvent(event);
|
||||
tryClose(this.#rid);
|
||||
} else if ("error" in message) {
|
||||
break;
|
||||
}
|
||||
case "error": {
|
||||
this.#readyState = CLOSED;
|
||||
|
||||
const errorEv = new ErrorEvent("error");
|
||||
|
@ -361,6 +370,8 @@
|
|||
closeEv.target = this;
|
||||
this.dispatchEvent(closeEv);
|
||||
tryClose(this.#rid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<u8>),
|
||||
|
|
Loading…
Reference in a new issue