diff --git a/index.js b/index.js index 1796ad6..9fab4da 100644 --- a/index.js +++ b/index.js @@ -122,6 +122,7 @@ class Connection { /** @type {NodeJS.Timer | null} */ reachabilityTimeout = null; + connected = false; writing = false; /** @@ -136,14 +137,22 @@ class Connection { } initTargetSocket() { + this.connected = false; clearTimeout(this.retryTimeout); this.writeSocket.on("connect", () => { + this.connected = true; console.log( "Opened new connection to target from", this.source.address, this.source.port ); + + if (this.msgBuffer.length > 0) { + requestIdleCallback(() => { + this.send(); + }); + } }); this.writeSocket.on("message", (fromMsg) => { this.#resetIdleTimeout(); @@ -180,7 +189,7 @@ class Connection { send(msg) { this.#resetIdleTimeout(); - if (msg && (this.writing || !this.writeSocket)) { + if (msg && (!this.connected || this.writing || !this.writeSocket)) { this.msgBuffer.push(msg); return; }