All checks were successful
continuous-integration/drone/push Build is passing
173 lines
6.0 KiB
JavaScript
173 lines
6.0 KiB
JavaScript
var Server = /** @class */ (function () {
|
|
function Server(cmd) {
|
|
this.cmd = cmd;
|
|
}
|
|
Server.prototype.request = function (data) {
|
|
if (SERVER_CONNECTION == true) {
|
|
return;
|
|
}
|
|
SERVER_CONNECTION = true;
|
|
console.log(data);
|
|
if (this.cmd != "updateLog") {
|
|
showElement("loading", true);
|
|
UNDO = new Object();
|
|
setConnectionState("busy");
|
|
}
|
|
switch (window.location.protocol) {
|
|
case "http:":
|
|
this.protocol = "ws://";
|
|
break;
|
|
case "https:":
|
|
this.protocol = "wss://";
|
|
break;
|
|
}
|
|
var wsHost = window.location.host;
|
|
if (wsHost == undefined || wsHost.length < 1) {
|
|
wsHost = window.location.hostname;
|
|
}
|
|
var url = this.protocol + wsHost + "/data/" + "?Token=" + getCookie("Token");
|
|
data["cmd"] = this.cmd;
|
|
var requestCmd = data["cmd"];
|
|
var ws = new WebSocket(url);
|
|
var isLogUpdate = data["cmd"] == "updateLog";
|
|
var responseReceived = false;
|
|
var requestFinished = false;
|
|
var timeoutMs = 12000;
|
|
var requestTimeout;
|
|
var finishRequest = function (state, responseSuccess) {
|
|
if (responseSuccess === void 0) { responseSuccess = false; }
|
|
if (requestFinished == true) {
|
|
return;
|
|
}
|
|
requestFinished = true;
|
|
SERVER_CONNECTION = false;
|
|
window.clearTimeout(requestTimeout);
|
|
if (responseSuccess == true) {
|
|
if (state == "online") {
|
|
WS_FAILURE_COUNT = 0;
|
|
}
|
|
}
|
|
else {
|
|
WS_FAILURE_COUNT++;
|
|
}
|
|
if (isLogUpdate == false) {
|
|
showElement("loading", false);
|
|
}
|
|
if (state != "") {
|
|
setConnectionState(state);
|
|
}
|
|
};
|
|
requestTimeout = window.setTimeout(function () {
|
|
console.log("Websocket request timed out.");
|
|
var timeoutState = "offline";
|
|
if (isLogUpdate == true && WS_FAILURE_COUNT < 2) {
|
|
timeoutState = "idle";
|
|
}
|
|
finishRequest(timeoutState, false);
|
|
try {
|
|
ws.close();
|
|
}
|
|
catch (err) {
|
|
console.log(err);
|
|
}
|
|
}, timeoutMs);
|
|
ws.onopen = function () {
|
|
WS_AVAILABLE = true;
|
|
if (data["cmd"] != "updateLog") {
|
|
setConnectionState("busy");
|
|
}
|
|
console.log("REQUEST (JS):");
|
|
console.log(data);
|
|
console.log("REQUEST: (JSON)");
|
|
console.log(JSON.stringify(data));
|
|
this.send(JSON.stringify(data));
|
|
};
|
|
ws.onerror = function (e) {
|
|
console.log("No websocket connection to xTeVe could be established. Check your network configuration.");
|
|
var errorState = "offline";
|
|
if (isLogUpdate == true && WS_FAILURE_COUNT < 2) {
|
|
errorState = "idle";
|
|
}
|
|
finishRequest(errorState, false);
|
|
if (WS_AVAILABLE == false && isLogUpdate == false && requestCmd != "getServerConfig") {
|
|
alert("No websocket connection to xTeVe could be established. Check your network configuration.");
|
|
}
|
|
};
|
|
ws.onmessage = function (e) {
|
|
responseReceived = true;
|
|
finishRequest("online", true);
|
|
console.log("RESPONSE:");
|
|
var response = JSON.parse(e.data);
|
|
console.log(response);
|
|
if (response.hasOwnProperty("token")) {
|
|
document.cookie = "Token=" + response["token"];
|
|
}
|
|
if (response["status"] == false) {
|
|
setConnectionState("offline");
|
|
alert(response["err"]);
|
|
if (response.hasOwnProperty("reload")) {
|
|
location.reload();
|
|
}
|
|
return;
|
|
}
|
|
if (response.hasOwnProperty("logoURL")) {
|
|
var div = document.getElementById("channel-icon");
|
|
div.value = response["logoURL"];
|
|
div.className = "changed";
|
|
return;
|
|
}
|
|
switch (data["cmd"]) {
|
|
case "updateLog":
|
|
SERVER["log"] = response["log"];
|
|
if (document.getElementById("content_log")) {
|
|
showLogs(false);
|
|
}
|
|
return;
|
|
break;
|
|
default:
|
|
SERVER = new Object();
|
|
SERVER = response;
|
|
break;
|
|
}
|
|
if (response.hasOwnProperty("openMenu")) {
|
|
var menu = document.getElementById(response["openMenu"]);
|
|
menu.click();
|
|
showElement("popup", false);
|
|
}
|
|
if (response.hasOwnProperty("openLink")) {
|
|
window.location = response["openLink"];
|
|
}
|
|
if (response.hasOwnProperty("alert")) {
|
|
alert(response["alert"]);
|
|
}
|
|
if (response.hasOwnProperty("reload")) {
|
|
location.reload();
|
|
}
|
|
if (response.hasOwnProperty("wizard")) {
|
|
createLayout();
|
|
configurationWizard[response["wizard"]].createWizard();
|
|
return;
|
|
}
|
|
createLayout();
|
|
};
|
|
ws.onclose = function () {
|
|
if (responseReceived == true) {
|
|
return;
|
|
}
|
|
var closeState = "offline";
|
|
if (isLogUpdate == true && WS_FAILURE_COUNT < 2) {
|
|
closeState = "idle";
|
|
}
|
|
finishRequest(closeState, false);
|
|
};
|
|
};
|
|
return Server;
|
|
}());
|
|
var WS_FAILURE_COUNT = 0;
|
|
function getCookie(name) {
|
|
var value = "; " + document.cookie;
|
|
var parts = value.split("; " + name + "=");
|
|
if (parts.length == 2)
|
|
return parts.pop().split(";").shift();
|
|
}
|