Skip to content

Commit 6066c11

Browse files
committed
Add display of ping
1 parent 5fddb37 commit 6066c11

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ <h4>Response</h4>
145145
</div>
146146
</main>
147147

148+
<div class="info-box">
149+
<div class="status">OK</div>
150+
<div class="ping"></div>
151+
</div>
152+
148153
<script type="module" src="/main.js"></script>
149154
</body>
150155
</html>

src/main.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const elms = {
1717
joystickZone: document.querySelector("#joystick-zone"),
1818
buttonsZone: document.querySelector("#buttons-zone"),
1919
},
20+
info: {
21+
statuses: [...document.querySelectorAll(".status")],
22+
pings: [...document.querySelectorAll(".ping")],
23+
},
2024
};
2125

2226
/// hamburgers
@@ -28,12 +32,33 @@ for (const burger of elms.hamburgers) {
2832
};
2933
}
3034

35+
/// info
36+
37+
function updateInfo(status, ping = Infinity) {
38+
for (const statusElm of elms.info.statuses) statusElm.innerText = status;
39+
for (const pingElm of elms.info.pings) pingElm.innerText = `ping: ${ping} ms`;
40+
}
41+
42+
updateInfo("200 OK", Date.now() - window.performance.timing.navigationStart);
43+
3144
/// poster
3245

3346
let host = params.get("host") ?? window.location.origin;
3447
let interval = Number(params.get("interval")) || 100;
3548
let showButtons = true;
3649

50+
async function callApi(method = "GET", path = "/", options = {}) {
51+
const prevTime = window.performance.now();
52+
try {
53+
const res = await fetch(host + path, { method, ...options });
54+
updateInfo(`${res.status} ${res.statusText}`, window.performance.now() - prevTime);
55+
return res;
56+
} catch (error) {
57+
updateInfo(error.message.split(/\s+/)[0], window.performance.now() - prevTime);
58+
throw error;
59+
}
60+
}
61+
3762
const joystickPoster = {
3863
_angle: Math.PI / 2,
3964
_force: 1,
@@ -54,14 +79,9 @@ const joystickPoster = {
5479

5580
async post() {
5681
console.log("POST joystick", { angle: this.angle, force: this.force, query: this.query });
57-
try {
58-
const res = await fetch(`${host}/joystick?q=${this.query}`, { method: "POST" });
59-
console.log("Responded joystick", await res.arrayBuffer(), await res.text());
60-
return res;
61-
} catch (error) {
62-
console.error(error);
63-
return null;
64-
}
82+
const res = await callApi("POST", "/joystick?q=" + this.query);
83+
console.log("Responded joystick", await res.text());
84+
return res;
6585
},
6686

6787
intervalId: null,
@@ -131,8 +151,8 @@ for (const button of elms.main.buttonsZone.childNodes) {
131151
button.onclick = async ({ target: button }) => {
132152
const id = button.innerText.toLowerCase();
133153
console.log("POST button", id);
134-
const res = await fetch(`${host}/${id}`, { method: "POST" });
135-
console.log("Responded button", await res.arrayBuffer(), await res.text());
154+
const res = await callApi("POST", "/" + id);
155+
console.log("Responded button", await res.text());
136156
};
137157
}
138158

src/style.css

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
.hamburger {
2222
position: absolute;
23+
top: 0;
24+
left: 0;
2325
padding: 0;
2426
height: 4rem;
2527
width: 4rem;
@@ -32,6 +34,7 @@
3234

3335
article {
3436
position: relative;
37+
top: 0;
3538
left: 0;
3639
padding: 2em;
3740
height: 100dvh;
@@ -131,3 +134,17 @@ main {
131134
background-color: #3f3;
132135
box-shadow: 0 0 0.5rem 0.25rem #3f35;
133136
}
137+
138+
/* info box */
139+
140+
.info-box {
141+
position: absolute;
142+
bottom: 0;
143+
left: 0;
144+
padding: .5em;
145+
width: 50%;
146+
z-index: -1;
147+
opacity: 0.5;
148+
pointer-events: none;
149+
cursor: not-allowed;
150+
}

0 commit comments

Comments
 (0)