mirror of
https://github.com/denoland/deno.git
synced 2025-01-10 16:11:13 -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() {
|
async #eventLoop() {
|
||||||
while (this.#readyState === OPEN) {
|
while (this.#readyState === OPEN) {
|
||||||
const message = await core.opAsync(
|
const { kind, value } = await core.opAsync(
|
||||||
"op_ws_next_event",
|
"op_ws_next_event",
|
||||||
this.#rid,
|
this.#rid,
|
||||||
);
|
);
|
||||||
|
|
||||||
if ("string" in message) {
|
switch (kind) {
|
||||||
|
case "string": {
|
||||||
const event = new MessageEvent("message", {
|
const event = new MessageEvent("message", {
|
||||||
data: message.string,
|
data: value,
|
||||||
origin: this.#url,
|
origin: this.#url,
|
||||||
});
|
});
|
||||||
event.target = this;
|
event.target = this;
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
} else if ("binary" in message) {
|
break;
|
||||||
|
}
|
||||||
|
case "binary": {
|
||||||
let data;
|
let data;
|
||||||
|
|
||||||
if (this.binaryType === "blob") {
|
if (this.binaryType === "blob") {
|
||||||
data = new Blob([new Uint8Array(message.binary)]);
|
data = new Blob([new Uint8Array(value)]);
|
||||||
} else {
|
} else {
|
||||||
data = new Uint8Array(message.binary).buffer;
|
data = new Uint8Array(value).buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
const event = new MessageEvent("message", {
|
const event = new MessageEvent("message", {
|
||||||
|
@ -334,23 +337,29 @@
|
||||||
});
|
});
|
||||||
event.target = this;
|
event.target = this;
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
} else if ("ping" in message) {
|
break;
|
||||||
|
}
|
||||||
|
case "ping": {
|
||||||
core.opAsync("op_ws_send", {
|
core.opAsync("op_ws_send", {
|
||||||
rid: this.#rid,
|
rid: this.#rid,
|
||||||
kind: "pong",
|
kind: "pong",
|
||||||
});
|
});
|
||||||
} else if ("close" in message) {
|
break;
|
||||||
|
}
|
||||||
|
case "close": {
|
||||||
this.#readyState = CLOSED;
|
this.#readyState = CLOSED;
|
||||||
|
|
||||||
const event = new CloseEvent("close", {
|
const event = new CloseEvent("close", {
|
||||||
wasClean: true,
|
wasClean: true,
|
||||||
code: message.close.code,
|
code: value.code,
|
||||||
reason: message.close.reason,
|
reason: value.reason,
|
||||||
});
|
});
|
||||||
event.target = this;
|
event.target = this;
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
tryClose(this.#rid);
|
tryClose(this.#rid);
|
||||||
} else if ("error" in message) {
|
break;
|
||||||
|
}
|
||||||
|
case "error": {
|
||||||
this.#readyState = CLOSED;
|
this.#readyState = CLOSED;
|
||||||
|
|
||||||
const errorEv = new ErrorEvent("error");
|
const errorEv = new ErrorEvent("error");
|
||||||
|
@ -361,6 +370,8 @@
|
||||||
closeEv.target = this;
|
closeEv.target = this;
|
||||||
this.dispatchEvent(closeEv);
|
this.dispatchEvent(closeEv);
|
||||||
tryClose(this.#rid);
|
tryClose(this.#rid);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -282,7 +282,7 @@ pub async fn op_ws_close(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(tag = "kind", content = "value", rename_all = "camelCase")]
|
||||||
pub enum NextEventResponse {
|
pub enum NextEventResponse {
|
||||||
String(String),
|
String(String),
|
||||||
Binary(Vec<u8>),
|
Binary(Vec<u8>),
|
||||||
|
|
Loading…
Reference in a new issue