This commit is contained in:
Percs 2024-09-04 19:04:27 -05:00
parent fae6b90225
commit 4b1c06d484

View file

@ -37,11 +37,14 @@ export class BareWebSocket extends EventTarget {
}; };
const event = new Event("open") const event = new Event("open")
this.dispatchEvent(event); this.dispatchEvent(event);
this.onopen(event); if (this.onopen) {
this.onopen(event);
}
}; };
const onmessage = async (payload) => { const onmessage = async (payload) => {
if ("byteLength" in payload) { if (typeof payload === "string") {
} else if ("byteLength" in payload) {
if (this.binaryType === "blob") { if (this.binaryType === "blob") {
payload = new Blob([payload]); payload = new Blob([payload]);
} else { } else {
@ -56,21 +59,27 @@ export class BareWebSocket extends EventTarget {
const event = new MessageEvent("message", {data: payload }); const event = new MessageEvent("message", {data: payload });
this.dispatchEvent(event); this.dispatchEvent(event);
this.onmessage(event); if (this.onmessage) {
this.onmessage(event);
}
}; };
const onclose = (code: number, reason: string) => { const onclose = (code: number, reason: string) => {
this.readyState = WebSocketFields.CLOSED; this.readyState = WebSocketFields.CLOSED;
const event = new CloseEvent("close", { code, reason }) const event = new CloseEvent("close", { code, reason })
this.dispatchEvent(event); this.dispatchEvent(event);
this.onclose(event) if (this.onclose) {
this.onclose(event);
}
}; };
const onerror = () => { const onerror = () => {
this.readyState = WebSocketFields.CLOSED; this.readyState = WebSocketFields.CLOSED;
const event = new Event("error"); const event = new Event("error");
this.dispatchEvent(event) this.dispatchEvent(event);
this.onerror(event) if (this.onerror) {
this.onerror(event);
};
}; };
this.channel = new MessageChannel(); this.channel = new MessageChannel();