Make the URL used to reach the server configurable (APP_PATH)

This commit is contained in:
philippe lhardy 2020-04-27 17:44:49 +00:00 committed by Raphaël Jakse
parent edda3fc6d8
commit f1606248bb
2 changed files with 13 additions and 4 deletions

View File

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

View File

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