From 68c12f22be29a1039fd7826dab63a971ca723007 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 25 Feb 2023 22:10:00 -0500 Subject: [PATCH] *Add option to emulate a websocket --- src/socket.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/socket.ts b/src/socket.ts index 507d061..ead2f42 100644 --- a/src/socket.ts +++ b/src/socket.ts @@ -16,6 +16,7 @@ interface SocketOptions { data: any, cb: Callback ) => void; + emulateWebsocket?: boolean; } export default class Socket extends Duplex { @@ -29,6 +30,7 @@ export default class Socket extends Duplex { declare readable: true; declare writable: true; public remotePublicKey: Uint8Array; + private _emulateWebsocket: boolean; constructor({ allowHalfOpen = false, @@ -36,12 +38,14 @@ export default class Socket extends Duplex { remotePort, remotePublicKey, write, + emulateWebsocket = false, }: SocketOptions = {}) { super({ write }); this._allowHalfOpen = allowHalfOpen; this.remoteAddress = remoteAddress; this.remotePort = remotePort; this.remotePublicKey = remotePublicKey; + this._emulateWebsocket = emulateWebsocket; if (remoteAddress) { const type = Socket.isIP(remoteAddress); @@ -51,6 +55,11 @@ export default class Socket extends Duplex { this.remoteFamily = type === 6 ? IPV6 : IPV4; } + + if (this._emulateWebsocket) { + // @ts-ignore + this.addEventListener("data", (data: any) => this.emit("message", data)); + } } private _connecting: boolean;