]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
webui: comet ws add reconnecting messages to tvheadend log
authorTrujulu <trujulu@gmail.com>
Wed, 7 Jan 2026 20:54:19 +0000 (21:54 +0100)
committerFlole <Flole998@users.noreply.github.com>
Sat, 10 Jan 2026 13:26:06 +0000 (14:26 +0100)
src/webui/static/app/comet.js

index 2f0e89eed7ba3a3395afcdb088525f2394905adb..e845b2a1bf5b6efe2018decc6f2cccc28bb4665e 100644 (file)
@@ -1,9 +1,7 @@
 /**
  * Comet interfaces
  */
-Ext.extend(tvheadend.Comet = function() {
-    this.addEvents({ });
-}, Ext.util.Observable);
+tvheadend.Comet = Ext.extend(Ext.util.Observable, {});
 
 tvheadend.comet = new tvheadend.Comet();
 tvheadend.ws = null;
@@ -17,26 +15,37 @@ tvheadend.cometError = function() {
             'font-weight: bold; color: #f00');
 };
 
+tvheadend.cometReconnecting = function() {
+    tvheadend.log(_('Reconnecting to Tvheadend...'),
+                    'font-weight: bold; color: #fa0');
+};
+
 tvheadend.cometReconnected = function() {
     tvheadend.log(_('Reconnected to Tvheadend'),
                     'font-weight: bold; color: #080');
-}
+};
 
 tvheadend.cometParse = function(responsetxt) {
-    var response = Ext.util.JSON.decode(responsetxt);
-    if ('boxid' in response) {
-        if (tvheadend.boxid && tvheadend.boxid !== response.boxid)
-            window.location.reload();
-        tvheadend.boxid = response.boxid;
-    }
-    for (x = 0; x < response.messages.length; x++) {
-        m = response.messages[x];
-        if (0) console.log(JSON.stringify(m), null, " ");
-        try {
-            tvheadend.comet.fireEvent(m.notificationClass, m);
-        } catch (e) {
-            tvheadend.log(_('Comet failure') + ' [e=' + e.message + ']');
+    try {
+        var response = Ext.util.JSON.decode(responsetxt);
+        if ('boxid' in response) {
+            if (tvheadend.boxid && tvheadend.boxid !== response.boxid)
+                window.location.reload();
+            tvheadend.boxid = response.boxid;
         }
+        if (Array.isArray(response.messages)) {
+            for (var x = 0; x < response.messages.length; x++) {
+                var m = response.messages[x];
+                //if (0) console.log(JSON.stringify(m), null, " ");
+                try {
+                    tvheadend.comet.fireEvent(m.notificationClass, m);
+                } catch (e) {
+                    tvheadend.log(_('Comet failure') + ' [e=' + e.message + ']');
+                }
+            }
+        }
+    } catch (e) {
+        tvheadend.log(_('Invalid JSON from comet') + ' [e=' + e.message + ']');
     }
 };
 
@@ -50,18 +59,21 @@ tvheadend.cometPoller = function() {
                 immediate: failures > 0 ? 1 : 0
             },
             success: function(result, request) {
-                tvheadend.cometParse(result.responseText);
-                cometRequest.delay(100);
-
                 if (failures > 1)
                     tvheadend.cometReconnected();
                 failures = 0;
+
+                tvheadend.cometParse(result.responseText);
+                cometRequest.delay(100);
             },
             failure: function(result, request) {
-                cometRequest.delay(failures ? 1000 : 1);
                 if (failures === 1)
                     tvheadend.cometError();
+                if (failures > 1)
+                    tvheadend.cometReconnecting();
                 failures++;
+
+                cometRequest.delay(failures ? 1000 : 1);
             }
         });
     });
@@ -78,11 +90,13 @@ tvheadend.cometWebsocket = function() {
         if (failures > 5)
             window.location.reload();
         if (failures > 1)
-            tvheadend.cometReconnected();
+            tvheadend.cometReconnecting();
         tvheadend.ws.onmessage = function(ev) {
+            if (failures > 1)
+                tvheadend.cometReconnected();
             failures = 0;
             tvheadend.cometParse(ev.data);
-        }
+        };
         tvheadend.ws.onerror = function(ev) {
             if (failures === 1)
                 tvheadend.cometError();
@@ -96,14 +110,13 @@ tvheadend.cometWebsocket = function() {
 };
 
 tvheadend.cometInit = function() {
-    var isSafari = navigator.vendor && navigator.vendor.indexOf('Apple') > -1 &&
-                   navigator.userAgent && !navigator.userAgent.match('CriOS');
+    var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
     if (!isSafari && ("WebSocket" in window)) {
       var loc = window.location;
       var path = loc.pathname.substring(0, loc.pathname.lastIndexOf("/"));
       tvheadend.wsURI = (loc.protocol === "https:" ? "wss:" : "ws:") + "//" + loc.host + path + "/comet/ws";
-      new tvheadend.cometWebsocket;
+      tvheadend.cometWebsocket();
     } else {
-      new tvheadend.cometPoller;
+      tvheadend.cometPoller();
     }
 };