diff --git a/dist/peer.js b/dist/peer.js index c4664c8..a116409 100644 --- a/dist/peer.js +++ b/dist/peer.js @@ -33,12 +33,21 @@ class Peer { const self = this; this._channel = await this._muxer.createChannel({ protocol: this._proxy.protocol, - onopen: this.handleChannelOnOpen.bind(this), - onclose: this.handleChannelOnClose.bind(this), + onopen: async (m) => { + await this.handleChannelOnOpen(m); + // @ts-ignore + await this._onopen?.(this._channel); + }, + onclose: async (socket) => { + await this.handleChannelOnClose(socket); + // @ts-ignore + await this._onclose?.(); + }, }); await this.initMessages(); await this._onchannel?.(this._channel); await this._channel.open(); + this._proxy.emit("peerChannelOpen", this); } async init() { await this.initSocket(); diff --git a/dist/proxies/basic/peer.js b/dist/proxies/basic/peer.js index 42910dd..3a809cb 100644 --- a/dist/proxies/basic/peer.js +++ b/dist/proxies/basic/peer.js @@ -41,7 +41,6 @@ class Peer extends peer_js_1.default { } async handleChannelOnClose(socket) { this._socket?.destroy(); - await this._onclose?.(this._socket); } async initMessages() { const self = this; diff --git a/dist/proxies/multiSocket.js b/dist/proxies/multiSocket.js index 0f9ffae..9f45830 100644 --- a/dist/proxies/multiSocket.js +++ b/dist/proxies/multiSocket.js @@ -149,7 +149,7 @@ class MultiSocketProxy extends proxy_js_1.default { } async _registerOpenSocketMessage(peer) { const self = this; - const message = peer.channel.addMessage({ + const message = await peer.channel.addMessage({ encoding: { preencode: compact_encoding_1.json.preencode, encode: compact_encoding_1.json.encode, @@ -183,7 +183,7 @@ class MultiSocketProxy extends proxy_js_1.default { } async _registerWriteSocketMessage(peer) { const self = this; - const message = peer.channel.addMessage({ + const message = await peer.channel.addMessage({ encoding: writeSocketEncoding, onmessage(m) { self._sockets.get(m.id)?.push(m.data); @@ -195,7 +195,7 @@ class MultiSocketProxy extends proxy_js_1.default { } async _registerCloseSocketMessage(peer) { const self = this; - const message = peer.channel.addMessage({ + const message = await peer.channel.addMessage({ encoding: socketEncoding, onmessage(m) { self._sockets.get(m.id)?.end(); @@ -207,7 +207,7 @@ class MultiSocketProxy extends proxy_js_1.default { } async _registerTimeoutSocketMessage(peer) { const self = this; - const message = peer.channel.addMessage({ + const message = await peer.channel.addMessage({ encoding: socketEncoding, onmessage(m) { // @ts-ignore @@ -220,7 +220,7 @@ class MultiSocketProxy extends proxy_js_1.default { } async _registerErrorSocketMessage(peer) { const self = this; - const message = peer.channel.addMessage({ + const message = await peer.channel.addMessage({ encoding: errorSocketEncoding, onmessage(m) { // @ts-ignore diff --git a/dist/proxies/multiSocket/peer.d.ts b/dist/proxies/multiSocket/peer.d.ts index f17c221..4529280 100644 --- a/dist/proxies/multiSocket/peer.d.ts +++ b/dist/proxies/multiSocket/peer.d.ts @@ -2,6 +2,7 @@ import BasePeer from "../../peer.js"; import Socket from "../../socket.js"; import MultiSocketProxy from "../multiSocket.js"; export default class Peer extends BasePeer { + protected initMessages(): Promise; protected _proxy: MultiSocketProxy; protected initSocket(): Promise; get stream(): any; diff --git a/dist/proxies/multiSocket/peer.js b/dist/proxies/multiSocket/peer.js index ae54057..8d211a1 100644 --- a/dist/proxies/multiSocket/peer.js +++ b/dist/proxies/multiSocket/peer.js @@ -5,6 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const peer_js_1 = __importDefault(require("../../peer.js")); class Peer extends peer_js_1.default { + async initMessages() { + await this._proxy.handleNewPeerChannel(this); + } async initSocket() { } get stream() { return this._muxer.stream; @@ -12,9 +15,6 @@ class Peer extends peer_js_1.default { async handleChannelOnClose(socket) { return this._proxy.handleClosePeer(this); } - async handleChannelOnOpen(m) { - await this._proxy.handleNewPeerChannel(this); - this._proxy.emit("peerOpen", this); - } + async handleChannelOnOpen(m) { } } exports.default = Peer;