From 1a26c6e910cfb84aec292c8e0371fe87968e9fee Mon Sep 17 00:00:00 2001 From: PaulPlay <80539587+therealPaulPlay@users.noreply.github.com> Date: Tue, 28 Jan 2025 17:38:06 +0100 Subject: [PATCH] Fix: Ensure outgoing (host) connection is open before sending message --- src/index.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 27db998..1219437 100644 --- a/src/index.js +++ b/src/index.js @@ -394,11 +394,13 @@ export default class PlayPeer { this.#broadcastMessage("storage_sync", { storage: this.#storage }); } else { try { - this.#outgoingConnection?.send({ - type: 'storage_update', - key, - value - }); + if (this.#outgoingConnection?.open) { + this.#outgoingConnection?.send({ + type: 'storage_update', + key, + value + }); + } } catch (error) { console.error(ERROR_PREFIX + "Error sending storage update to host:", error); this.#triggerEvent("error", "Error sending storage update to host: " + error); @@ -481,13 +483,15 @@ export default class PlayPeer { } else { try { // Request the host to perform the operation - this.#outgoingConnection?.send({ - type: 'array_update', - key, - operation, - value, - updateValue - }); + if (this.#outgoingConnection?.open) { + this.#outgoingConnection?.send({ + type: 'array_update', + key, + operation, + value, + updateValue + }); + } this.#handleArrayUpdate(key, operation, value, updateValue); // Optimistic update } catch (error) { console.error(ERROR_PREFIX + `Failed to send array update to host:`, error);