Allow configuring the client

This commit is contained in:
Raphaël Jakse 2020-04-15 22:15:03 +02:00
parent f8426578ad
commit 0cce58e5b0
4 changed files with 40 additions and 10 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/games.backup.json
/config.js

19
config.js.sample Normal file
View File

@ -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
};

View 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>

View File

@ -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;
}