mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
patch up fake websocket
This commit is contained in:
parent
09a2238fec
commit
de7de48706
2 changed files with 6 additions and 12 deletions
|
@ -159,8 +159,8 @@ export class BareClient {
|
||||||
requestHeaders['Connection'] = 'Upgrade';
|
requestHeaders['Connection'] = 'Upgrade';
|
||||||
|
|
||||||
const socket = new BareWebSocket(remote, protocols, this.worker, requestHeaders, arrayBufferImpl)
|
const socket = new BareWebSocket(remote, protocols, this.worker, requestHeaders, arrayBufferImpl)
|
||||||
|
|
||||||
return socket as WebSocket;
|
return socket as unknown as WebSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch(
|
async fetch(
|
||||||
|
|
|
@ -14,10 +14,6 @@ export class BareWebSocket extends EventTarget {
|
||||||
onmessage = (event: MessageEvent) => {};
|
onmessage = (event: MessageEvent) => {};
|
||||||
onclose = (event: CloseEvent) => {};
|
onclose = (event: CloseEvent) => {};
|
||||||
|
|
||||||
CONNECTING = WebSocketFields.CONNECTING;
|
|
||||||
OPEN = WebSocketFields.OPEN;
|
|
||||||
CLOSING = WebSocketFields.CLOSING
|
|
||||||
CLOSED = WebSocketFields.CLOSED;
|
|
||||||
channel: MessageChannel;
|
channel: MessageChannel;
|
||||||
constructor(
|
constructor(
|
||||||
remote: string | URL,
|
remote: string | URL,
|
||||||
|
@ -33,7 +29,7 @@ export class BareWebSocket extends EventTarget {
|
||||||
const onopen = (protocol: string) => {
|
const onopen = (protocol: string) => {
|
||||||
this.readyState = WebSocketFields.OPEN;
|
this.readyState = WebSocketFields.OPEN;
|
||||||
this.protocols = protocol;
|
this.protocols = protocol;
|
||||||
|
|
||||||
(this as any).meta = {
|
(this as any).meta = {
|
||||||
headers: {
|
headers: {
|
||||||
"sec-websocket-protocol": protocol,
|
"sec-websocket-protocol": protocol,
|
||||||
|
@ -45,9 +41,7 @@ export class BareWebSocket extends EventTarget {
|
||||||
};
|
};
|
||||||
|
|
||||||
const onmessage = async (payload) => {
|
const onmessage = async (payload) => {
|
||||||
if (typeof payload === "string") {
|
if ("byteLength" in payload) {
|
||||||
console.log("string strung")
|
|
||||||
} else if ("byteLength" in payload) {
|
|
||||||
if (this.binaryType === "blob") {
|
if (this.binaryType === "blob") {
|
||||||
payload = new Blob([payload]);
|
payload = new Blob([payload]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -60,7 +54,7 @@ export class BareWebSocket extends EventTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let event = new MessageEvent("message", {data: payload });
|
const event = new MessageEvent("message", {data: payload });
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
this.onmessage(event);
|
this.onmessage(event);
|
||||||
};
|
};
|
||||||
|
@ -120,7 +114,7 @@ export class BareWebSocket extends EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
close(code, reason) {
|
close(code, reason) {
|
||||||
this.readyState = this.CLOSING;
|
this.readyState = WebSocketFields.CLOSING;
|
||||||
this.channel.port1.postMessage({ type: "close", closeCode: code, closeReason: reason });
|
this.channel.port1.postMessage({ type: "close", closeCode: code, closeReason: reason });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue