From d4de3940036672d7527984bbd99d5c0155bccedb Mon Sep 17 00:00:00 2001 From: zoton2 Date: Wed, 12 Jan 2022 08:12:52 +0000 Subject: [PATCH] streamdeck-plugin-vue: some changes to backend class --- .gitignore | 2 +- streamdeck-plugin-vue/src/backend/backend.ts | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 7d0a4e2..a1d9c9d 100644 --- a/.gitignore +++ b/.gitignore @@ -129,6 +129,6 @@ dist .yarn/install-state.gz .pnp.* -streamdeck-plugin/Release/com.zoton2.example.streamDeckPlugin +com.zoton2.example.streamDeckPlugin DistributionTool.exe streamdeck-plugin-vue/com.zoton2.example.sdPlugin diff --git a/streamdeck-plugin-vue/src/backend/backend.ts b/streamdeck-plugin-vue/src/backend/backend.ts index 95bf731..0a6e6b8 100644 --- a/streamdeck-plugin-vue/src/backend/backend.ts +++ b/streamdeck-plugin-vue/src/backend/backend.ts @@ -133,6 +133,16 @@ class Backend extends EventEmitter { }); } + /** + * Helper function to send messages to the Stream Deck WebSocket server if connection is ready. + */ + sendToSDWS(data: unknown): void { + if (this.sdWS && this.sdWS.readyState === 1) { + const str = typeof data !== 'string' ? JSON.stringify(data) : data; + this.sdWS.send(str); + } + } + /** * node-streamdeck-util connection. */ @@ -163,15 +173,13 @@ class Backend extends EventEmitter { console.warn('Connection to node-streamdeck-util server closed (%s)', e.code); this.toggleBackendConnectionStatus(false); clearTimeout(this.serverWSReconnTimeout); - this.serverWSReconnTimeout = setTimeout(this.connectToServerWS, 5000); + this.serverWSReconnTimeout = setTimeout(() => { this.connectToServerWS(); }, 5000); }, { once: true }); // Relays any messages sent from the node-streamdeck-util server to the main socket. this.serverWS.addEventListener('message', (e) => { const { data } = e; - if (this.sdWS && this.sdWS.readyState === 1) { - this.sdWS.send(data); - } + this.sendToSDWS(data); }); }