*Add option to emulate a websocket
This commit is contained in:
parent
11550659d0
commit
68c12f22be
|
@ -16,6 +16,7 @@ interface SocketOptions {
|
||||||
data: any,
|
data: any,
|
||||||
cb: Callback
|
cb: Callback
|
||||||
) => void;
|
) => void;
|
||||||
|
emulateWebsocket?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Socket extends Duplex {
|
export default class Socket extends Duplex {
|
||||||
|
@ -29,6 +30,7 @@ export default class Socket extends Duplex {
|
||||||
declare readable: true;
|
declare readable: true;
|
||||||
declare writable: true;
|
declare writable: true;
|
||||||
public remotePublicKey: Uint8Array;
|
public remotePublicKey: Uint8Array;
|
||||||
|
private _emulateWebsocket: boolean;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
allowHalfOpen = false,
|
allowHalfOpen = false,
|
||||||
|
@ -36,12 +38,14 @@ export default class Socket extends Duplex {
|
||||||
remotePort,
|
remotePort,
|
||||||
remotePublicKey,
|
remotePublicKey,
|
||||||
write,
|
write,
|
||||||
|
emulateWebsocket = false,
|
||||||
}: SocketOptions = {}) {
|
}: SocketOptions = {}) {
|
||||||
super({ write });
|
super({ write });
|
||||||
this._allowHalfOpen = allowHalfOpen;
|
this._allowHalfOpen = allowHalfOpen;
|
||||||
this.remoteAddress = remoteAddress;
|
this.remoteAddress = remoteAddress;
|
||||||
this.remotePort = remotePort;
|
this.remotePort = remotePort;
|
||||||
this.remotePublicKey = remotePublicKey;
|
this.remotePublicKey = remotePublicKey;
|
||||||
|
this._emulateWebsocket = emulateWebsocket;
|
||||||
|
|
||||||
if (remoteAddress) {
|
if (remoteAddress) {
|
||||||
const type = Socket.isIP(remoteAddress);
|
const type = Socket.isIP(remoteAddress);
|
||||||
|
@ -51,6 +55,11 @@ export default class Socket extends Duplex {
|
||||||
|
|
||||||
this.remoteFamily = type === 6 ? IPV6 : IPV4;
|
this.remoteFamily = type === 6 ? IPV6 : IPV4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this._emulateWebsocket) {
|
||||||
|
// @ts-ignore
|
||||||
|
this.addEventListener("data", (data: any) => this.emit("message", data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _connecting: boolean;
|
private _connecting: boolean;
|
||||||
|
|
Loading…
Reference in New Issue