From cbf6877560dabe2dc00e2665317ae3add5b82b4a Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Sat, 14 Oct 2023 22:09:44 +0200
Subject: [PATCH] #196 - Send ping message every minute to PlayWall

---
 main.js | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/main.js b/main.js
index 349c1bc..e1132e9 100644
--- a/main.js
+++ b/main.js
@@ -50,6 +50,10 @@ class ModuleInstance extends InstanceBase {
             clearTimeout(this.reconnect_timer)
             this.reconnect_timer = null
         }
+        if (this.ping_timer) {
+            clearTimeout(this.ping_timer)
+            this.ping_timer = null
+        }
         if (this.ws) {
             this.ws.close(1000)
             delete this.ws
@@ -119,11 +123,27 @@ class ModuleInstance extends InstanceBase {
         }
     }
 
+    startPingTrigger() {
+        if (this.isInitialized) {
+            if (this.ping_timer) {
+                clearTimeout(this.ping_timer)
+            }
+            this.log('debug', "Start Ping Timer");
+            this.ping_timer = setInterval(() => {
+                this.sendToWebSocket('ping', {});
+            }, 60 * 1000)
+        }
+    }
+
     initWebSocket() {
         if (this.reconnect_timer) {
             clearTimeout(this.reconnect_timer)
             this.reconnect_timer = null
         }
+        if (this.ping_timer) {
+            clearTimeout(this.ping_timer)
+            this.ping_timer = null
+        }
 
         if (this.config.host == null || this.config.port == null) {
             this.log('debug', `PlayWall host '${this.config.host}' or port '${this.config.port}' is invalid`);
@@ -148,6 +168,7 @@ class ModuleInstance extends InstanceBase {
         this.ws.on('open', () => {
             this.updateStatus(InstanceStatus.Ok)
             this.log('debug', `Connection opened`)
+            this.startPingTrigger();
 
             this.sendToWebSocket('project-current', {});
         })
-- 
GitLab