general improvements for connection checking

Signed-off-by: Victor Seiji Hariki <victorseijih@gmail.com>
This commit is contained in:
Victor Seiji Hariki 2022-11-30 18:47:30 -03:00
parent 6d44db5d66
commit 6f99b15320
No known key found for this signature in database
GPG key ID: F369E3EA50A0DEEE
2 changed files with 49 additions and 15 deletions

View file

@ -197,6 +197,10 @@ body {
background-color: #dddd49; background-color: #dddd49;
} }
.host-field-wrapper .connection-status.before {
background-color: #777;
}
input#host { input#host {
box-sizing: border-box; box-sizing: border-box;
} }

View file

@ -82,18 +82,19 @@ var marchCoords = {};
// //
function startup() { function startup() {
testHostConfiguration(); testHostConfiguration();
testHostConnection();
loadSettings(); loadSettings();
const hostEl = document.getElementById("host"); const hostEl = document.getElementById("host");
hostEl.onchange = () => { testHostConnection().then((checkConnection) => {
host = hostEl.value.endsWith("/") hostEl.onchange = () => {
? hostEl.value.substring(0, hostEl.value.length - 1) host = hostEl.value.endsWith("/")
: hostEl.value; ? hostEl.value.substring(0, hostEl.value.length - 1)
hostEl.value = host; : hostEl.value;
localStorage.setItem("host", host); hostEl.value = host;
}; localStorage.setItem("host", host);
checkConnection();
};
});
const promptEl = document.getElementById("prompt"); const promptEl = document.getElementById("prompt");
promptEl.oninput = () => { promptEl.oninput = () => {
@ -157,7 +158,7 @@ function testHostConfiguration() {
} }
} }
function testHostConnection() { async function testHostConnection() {
class CheckInProgressError extends Error {} class CheckInProgressError extends Error {}
const connectionIndicator = document.getElementById( const connectionIndicator = document.getElementById(
@ -174,6 +175,7 @@ function testHostConnection() {
connectionIndicator.classList.remove( connectionIndicator.classList.remove(
"cors-issue", "cors-issue",
"offline", "offline",
"before",
"server-error" "server-error"
); );
connectionIndicator.title = "Connected"; connectionIndicator.title = "Connected";
@ -181,7 +183,12 @@ function testHostConnection() {
}, },
error: () => { error: () => {
connectionIndicator.classList.add("server-error"); connectionIndicator.classList.add("server-error");
connectionIndicator.classList.remove("online", "offline", "cors-issue"); connectionIndicator.classList.remove(
"online",
"offline",
"before",
"cors-issue"
);
connectionIndicator.title = connectionIndicator.title =
"Server is online, but is returning an error response"; "Server is online, but is returning an error response";
connectionStatus = false; connectionStatus = false;
@ -191,6 +198,7 @@ function testHostConnection() {
connectionIndicator.classList.remove( connectionIndicator.classList.remove(
"online", "online",
"offline", "offline",
"before",
"server-error" "server-error"
); );
connectionIndicator.title = connectionIndicator.title =
@ -202,17 +210,31 @@ function testHostConnection() {
connectionIndicator.classList.remove( connectionIndicator.classList.remove(
"cors-issue", "cors-issue",
"online", "online",
"before",
"server-error" "server-error"
); );
connectionIndicator.title = connectionIndicator.title =
"Server seems to be offline. Please check the console for more information."; "Server seems to be offline. Please check the console for more information.";
connectionStatus = false; connectionStatus = false;
}, },
before: () => {
connectionIndicator.classList.add("before");
connectionIndicator.classList.remove(
"cors-issue",
"online",
"offline",
"server-error"
);
connectionIndicator.title = "Waiting for check to complete.";
connectionStatus = false;
},
}; };
statuses[status] && statuses[status](); statuses[status] && statuses[status]();
}; };
setConnectionStatus("before");
let checkInProgress = false; let checkInProgress = false;
const checkConnection = async (notify = false) => { const checkConnection = async (notify = false) => {
@ -224,7 +246,10 @@ function testHostConnection() {
var url = document.getElementById("host").value + "/startup-events"; var url = document.getElementById("host").value + "/startup-events";
// Attempt normal request // Attempt normal request
try { try {
const response = await fetch(url); /** @type {Response} */
const response = await fetch(url, {
signal: AbortSignal.timeout(5000),
});
if (response.status === 200) { if (response.status === 200) {
setConnectionStatus("online"); setConnectionStatus("online");
@ -243,6 +268,7 @@ function testHostConnection() {
} }
} catch (e) { } catch (e) {
try { try {
if (e instanceof DOMException) throw "offline";
// Tests if problem is CORS // Tests if problem is CORS
await fetch(url, {mode: "no-cors"}); await fetch(url, {mode: "no-cors"});
@ -266,7 +292,7 @@ function testHostConnection() {
return status; return status;
}; };
checkConnection(true); await checkConnection(true);
// On click, attempt to refresh // On click, attempt to refresh
connectionIndicator.onclick = async () => { connectionIndicator.onclick = async () => {
@ -281,8 +307,8 @@ function testHostConnection() {
// Checks every 5 seconds if offline, 30 seconds if online // Checks every 5 seconds if offline, 30 seconds if online
const checkAgain = () => { const checkAgain = () => {
setTimeout( setTimeout(
() => { async () => {
checkConnection(); await checkConnection();
checkAgain(); checkAgain();
}, },
connectionStatus ? 30000 : 5000 connectionStatus ? 30000 : 5000
@ -290,6 +316,10 @@ function testHostConnection() {
}; };
checkAgain(); checkAgain();
return () => {
checkConnection().catch(() => {});
};
} }
function newImage(evt) { function newImage(evt) {