*Add onchannel to allow creating additional messages

This commit is contained in:
Derrick Hammer 2023-03-03 05:22:29 -05:00
parent b14509d6f1
commit 6c6f6c4954
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 10 additions and 0 deletions

View File

@ -14,6 +14,7 @@ export type OnData = (peer: Peer, data: any) => void;
export type OnReceive = OnData; export type OnReceive = OnData;
export type OnClose = OnData; export type OnClose = OnData;
export type OnSend = OnData; export type OnSend = OnData;
export type OnChannel = (peer: Peer, channel: any) => void;
export type OnOpenBound = ( export type OnOpenBound = (
socket: Socket, socket: Socket,
@ -28,11 +29,14 @@ export type OnReceiveBound = OnDataBound;
export type OnCloseBound = OnDataBound; export type OnCloseBound = OnDataBound;
export type OnSendBound = OnDataBound; export type OnSendBound = OnDataBound;
export type OnChannelBound = (channel: any) => void;
export interface DataSocketOptions { export interface DataSocketOptions {
onopen?: OnOpen; onopen?: OnOpen;
onreceive?: OnReceive; onreceive?: OnReceive;
onsend?: OnSend; onsend?: OnSend;
onclose?: OnClose; onclose?: OnClose;
onchannel?: OnChannel;
emulateWebsocket?: boolean; emulateWebsocket?: boolean;
} }
@ -54,6 +58,7 @@ export default class Peer {
private _onreceive: OnReceiveBound; private _onreceive: OnReceiveBound;
private _onsend: OnSendBound; private _onsend: OnSendBound;
private _onclose: OnCloseBound; private _onclose: OnCloseBound;
private _onchannel: OnChannelBound;
private _emulateWebsocket: boolean; private _emulateWebsocket: boolean;
constructor({ constructor({
@ -64,6 +69,7 @@ export default class Peer {
onreceive, onreceive,
onsend, onsend,
onclose, onclose,
onchannel,
emulateWebsocket = false, emulateWebsocket = false,
}: PeerOptionsWithProxy & DataSocketOptions) { }: PeerOptionsWithProxy & DataSocketOptions) {
this._proxy = proxy; this._proxy = proxy;
@ -73,6 +79,7 @@ export default class Peer {
this._onreceive = onreceive?.bind(undefined, this); this._onreceive = onreceive?.bind(undefined, this);
this._onsend = onsend?.bind(undefined, this); this._onsend = onsend?.bind(undefined, this);
this._onclose = onclose?.bind(undefined, this); this._onclose = onclose?.bind(undefined, this);
this._onchannel = onchannel?.bind(undefined, this);
this._emulateWebsocket = emulateWebsocket; this._emulateWebsocket = emulateWebsocket;
} }
@ -130,6 +137,7 @@ export default class Peer {
}, },
}); });
await this._onchannel?.(this._channel);
await this._channel.open(); await this._channel.open();
} }
} }

View File

@ -20,6 +20,7 @@ export default class Proxy {
onreceive, onreceive,
onsend, onsend,
onclose, onclose,
onchannel,
listen = false, listen = false,
autostart = false, autostart = false,
emulateWebsocket = false, emulateWebsocket = false,
@ -33,6 +34,7 @@ export default class Proxy {
onreceive, onreceive,
onsend, onsend,
onclose, onclose,
onchannel,
emulateWebsocket, emulateWebsocket,
}; };
this.init(); this.init();