Allow configuring the client
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Wed, 15 Apr 2020 20:15:03 +0000 (22:15 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Wed, 15 Apr 2020 20:15:03 +0000 (22:15 +0200)
.gitignore
config.js.sample [new file with mode: 0644]
index.html
trivabble.js

index 4b2519fbc63b18e2c4bce84ed98b1ab07e48b234..44e8497a75e4ee0bebd52195d3dfe98ceeb6693c 100644 (file)
@@ -1 +1,2 @@
 /games.backup.json
+/config.js
diff --git a/config.js.sample b/config.js.sample
new file mode 100644 (file)
index 0000000..5c05a11
--- /dev/null
@@ -0,0 +1,19 @@
+// This is a sample config file. Copy to config.js if you need to fiddle with the
+// configuration of the client.
+
+window.TrivabbleConf = {
+    // The amount of time to wait after a connection failure
+    POLLING_DELAY:        2000,
+
+    // Whether WebSockets should be used, if possible
+    ENABLE_WEBSOCKETS:    true,
+
+    // Whether Server Sent Events should be used, if available
+    ENABLE_EVENT_SOURCE:  true,
+
+    // Max consecutive tries before blacklisting WebSockets for the current session
+    MAX_WEBSOCKET_ERRORS": 1,
+
+    // I don't like trailing commas, here is a nice message for you reading this file :-)
+    HAVE_FUN: true
+};
index 3a9899a3e341b8d6d8870ee6f391808c441d3c43..d4822c693896d625f1b02a5d49a775c6c4c72fe3 100644 (file)
@@ -8,6 +8,7 @@
         <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
         <meta name="apple-mobile-web-app-capable" content="yes" />
         <meta name="format-detection" content="telephone=no" />
+        <script src="config.js"></script>
         <script src="l10n.js"></script>
         <script src="alert.js"></script>
         <script src="touch2click.js"></script>
index 38072cc55fe4993cbf60c2ef8200cb36f897afff..911b323ecfdf13a2e54eb2ed704d9ba61634e5be 100644 (file)
     "use strict";
 
     const VERSION = 202004121400;
-    const POLLING_DELAY = 2000;
-    const ENABLE_WEBSOCKETS = true;
-    const ENABLE_EVENT_SOURCE = true;
-    const MAX_WEBSOCKET_ERRORS = 1;
+
+    const Conf = window.TrivabbleConf || {};
+
+    function setConf(parameterName, defaultValue) {
+        if (typeof Conf[parameterName] === "undefined") {
+            Conf[parameterName] = defaultValue;
+        }
+    }
+
+    setConf("POLLING_DELAY",        2000);
+    setConf("ENABLE_WEBSOCKETS",    true);
+    setConf("ENABLE_EVENT_SOURCE",  true);
+    setConf("MAX_WEBSOCKET_ERRORS", 1);
 
     const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {return s;};
 
                     startConnection();
                     retryPollingTimeout = 0;
                 },
-                delay || POLLING_DELAY
+                delay || Conf.POLLING_DELAY
             );
         } else {
             retriedImmediately = true;
             } else {
                 webSocketErrors++;
 
-                if (webSocketErrors > MAX_WEBSOCKET_ERRORS) {
+                if (webSocketErrors > Conf.MAX_WEBSOCKET_ERRORS) {
                     blacklistWebsockets = true;
                 }
             }
     }
 
     function pollServerWithEventSource() {
-        if (canConnect() && ENABLE_EVENT_SOURCE && window.EventSource) {
+        if (canConnect() && Conf.ENABLE_EVENT_SOURCE && window.EventSource) {
             closeConnections();
             pollingServer = true;
 
     }
 
     function pollServerWithWebSocket() {
-        if (canConnect() && ENABLE_WEBSOCKETS && !blacklistWebsockets && window.WebSocket) {
+        if (canConnect() && Conf.ENABLE_WEBSOCKETS && !blacklistWebsockets && window.WebSocket) {
             closeConnections();
             pollingServer = true;
 
         xhrRequest(cmdsWithContext(cmds), function (xhr) {
             if (xhr.readyState === 4) {
                 if (xhr.status === 0 || xhr.status >= 300) {
-                    setTimeout(sendCmds.bind(null, cmds), POLLING_DELAY);
+                    setTimeout(sendCmds.bind(null, cmds), Conf.POLLING_DELAY);
                     return;
                 }
 
 
         if (!pollingReady) {
             startConnection();
-            setTimeout(sendCmds, POLLING_DELAY, cmds);
+            setTimeout(sendCmds, Conf.POLLING_DELAY, cmds);
             return;
         }