From: philippe lhardy Date: Mon, 27 Apr 2020 17:44:49 +0000 (+0000) Subject: Make the URL used to reach the server configurable (APP_PATH) X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=f1606248bb4a250fc745fc63c49a21ebbc9ab69f;p=trivabble.git Make the URL used to reach the server configurable (APP_PATH) --- diff --git a/config.js.sample b/config.js.sample index 971c55b..beba4ee 100644 --- a/config.js.sample +++ b/config.js.sample @@ -14,6 +14,9 @@ window.TrivabbleConf = { // Max consecutive tries before blacklisting WebSockets for the current session MAX_WEBSOCKET_ERRORS: 1, + // to tweak only if your webserver is shared with other conflicting resources at / ( ex: yunohost integration ) + APP_PATH: "", + // I don't like trailing commas, here is a nice message for you reading this file :-) HAVE_FUN: true }; diff --git a/trivabble.js b/trivabble.js index b26131a..e832002 100644 --- a/trivabble.js +++ b/trivabble.js @@ -27,7 +27,7 @@ (function () { "use strict"; - const VERSION = 202004222300; + const VERSION = 202004262313; const Conf = window.TrivabbleConf || {}; @@ -41,6 +41,7 @@ setConf("ENABLE_WEBSOCKETS", true); setConf("ENABLE_EVENT_SOURCE", true); setConf("MAX_WEBSOCKET_ERRORS", 1); + setConf("APP_PATH", ""); const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {return s;}; @@ -85,6 +86,11 @@ let pollingServer = false; let connectionStopped = false; + // HTTP path url prefix for any socket (xhr,ess or ws) from browser to server access + function getApiEntryPoint() { + return Conf.APP_PATH + "/:trivabble"; + } + function mouseDown(ele, fun, stop) { const meth = stop ? "removeEventListener" : "addEventListener"; ele[meth]("mousedown", fun, false); @@ -991,7 +997,7 @@ closeConnections(); pollingServer = true; - eventSource = new EventSource("/:trivabble/sse/" + JSON.stringify(cmdsWithContext())); + eventSource = new EventSource(getApiEntryPoint() + "/sse/" + JSON.stringify(cmdsWithContext())); bindConnectionEvents(eventSource); return; } @@ -1007,7 +1013,7 @@ webSocket = new WebSocket( (window.location.protocol === "http:" ? "ws://" : "wss://") + window.location.host + - "/:trivabble/ws/" + + getApiEntryPoint() + "/ws/" + JSON.stringify(cmdsWithContext()) ); @@ -1021,7 +1027,7 @@ function xhrRequest(data, onreadystatechange) { const xhr = new XMLHttpRequest(); - xhr.open("POST", "/:trivabble", true); + xhr.open("POST", getApiEntryPoint(), true); xhr.setRequestHeader("Content-Type", "text/plain"); xhr.send(JSON.stringify(data));