From 0cce58e5b0b2b7ea537059ae24ee7a730422fdde Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Jakse?= Date: Wed, 15 Apr 2020 22:15:03 +0200 Subject: [PATCH] Allow configuring the client --- .gitignore | 1 + config.js.sample | 19 +++++++++++++++++++ index.html | 1 + trivabble.js | 29 +++++++++++++++++++---------- 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 config.js.sample diff --git a/.gitignore b/.gitignore index 4b2519f..44e8497 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /games.backup.json +/config.js diff --git a/config.js.sample b/config.js.sample new file mode 100644 index 0000000..5c05a11 --- /dev/null +++ b/config.js.sample @@ -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 +}; diff --git a/index.html b/index.html index 3a9899a..d4822c6 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,7 @@ + diff --git a/trivabble.js b/trivabble.js index 38072cc..911b323 100644 --- a/trivabble.js +++ b/trivabble.js @@ -28,10 +28,19 @@ "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;}; @@ -795,7 +804,7 @@ startConnection(); retryPollingTimeout = 0; }, - delay || POLLING_DELAY + delay || Conf.POLLING_DELAY ); } else { retriedImmediately = true; @@ -864,7 +873,7 @@ } else { webSocketErrors++; - if (webSocketErrors > MAX_WEBSOCKET_ERRORS) { + if (webSocketErrors > Conf.MAX_WEBSOCKET_ERRORS) { blacklistWebsockets = true; } } @@ -891,7 +900,7 @@ } function pollServerWithEventSource() { - if (canConnect() && ENABLE_EVENT_SOURCE && window.EventSource) { + if (canConnect() && Conf.ENABLE_EVENT_SOURCE && window.EventSource) { closeConnections(); pollingServer = true; @@ -904,7 +913,7 @@ } function pollServerWithWebSocket() { - if (canConnect() && ENABLE_WEBSOCKETS && !blacklistWebsockets && window.WebSocket) { + if (canConnect() && Conf.ENABLE_WEBSOCKETS && !blacklistWebsockets && window.WebSocket) { closeConnections(); pollingServer = true; @@ -1006,7 +1015,7 @@ 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; } @@ -1038,7 +1047,7 @@ if (!pollingReady) { startConnection(); - setTimeout(sendCmds, POLLING_DELAY, cmds); + setTimeout(sendCmds, Conf.POLLING_DELAY, cmds); return; } -- 2.30.2