From 82eb9825bd0b21cdf6e20e570a549c388463015c Mon Sep 17 00:00:00 2001 From: lou Date: Sat, 12 Oct 2024 19:48:48 -0400 Subject: [PATCH] add status js --- src/js/status.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/js/status.js diff --git a/src/js/status.js b/src/js/status.js new file mode 100644 index 0000000..b333a92 --- /dev/null +++ b/src/js/status.js @@ -0,0 +1,64 @@ +async function ping(url) { + const start = performance.now(); + try { + await fetch(url); + const end = performance.now(); + return end - start; + } catch (error) { + console.error(`Error pinging ${url}:`, error); + return Infinity; + } +} + +async function testPing() { + const urls = [ + 'https://aidxn.fun/ping', + 'https://kantor.aidxn.fun/ping', + 'https://api.aidxn.fun/ping' + ]; + + const pingResults = await Promise.all( + urls.map(async (url) => { + const time = await ping(url); + if (url === 'https://aidxn.fun/ping') { + const website = document.getElementById("website"); + website.textContent = `[ONLINE - ${time} ms]` + } + if (url === 'https://kantor.aidxn.fun/ping') { + const status1 = document.getElementById("status1"); + status1.textContent = `[ONLINE - ${time} ms]` + status1.class = 'text-green-500 font-bold'; + } + if (url === 'https://api.aidxn.fun/ping') { + const api = document.getElementById("api"); + api.textContent = `[ONLINE - ${time} ms]` + api.class = 'text-green-500 font-bold'; + } + return { url, time }; + }) + ); + + pingResults.sort((a, b) => a.time - b.time); + + console.log('Fastest server:', pingResults[0].url); + pingResults.forEach(result => { + console.log(`${result.url}: ${result.time.toFixed(2)} ms`); + }); + const fastestServer = pingResults[0].url; + if (fastestServer === 'https://aidxn.fun/ping') { + oldText = website.textContent; + website.textContent = oldText + ' [FASTEST]'; + } + if (fastestServer === 'https://kantor.aidxn.fun/ping') { + oldText = status1.textContent; + status1.textContent = oldText + ' [FASTEST]'; + } + if (fastestServer === 'https://api.aidxn.fun/ping') { + oldText = api.textContent; + api.textContent = oldText + ' [FASTEST]'; + } +} + +window.onload = () => { + testPing(); +}; \ No newline at end of file