From 6044a3e50cc64eff66664a719ace1b6438b16176 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Tue, 25 Jun 2024 12:58:11 +0200 Subject: [PATCH] feat(client): :sparkles: add function to test connection on ip update --- client/index.html | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/client/index.html b/client/index.html index d6ee267..a6c748e 100644 --- a/client/index.html +++ b/client/index.html @@ -116,7 +116,7 @@ document .querySelector('#settings') - .addEventListener('submit', (event) => { + .addEventListener('submit', async (event) => { // Don't interrupt event if not form submitting if (!(event instanceof SubmitEvent)) return true if (event.target === null) return true @@ -128,9 +128,38 @@ if (ipAddress === null) return - setEndpoint(`http://${ipAddress}`) + const endpoint = `http://${ipAddress}` + await testEndpoint(endpoint) + setEndpoint(endpoint) }) + /** + * Test connection to endpoint. + * + * @param {string} endpoint Endpoint to test for. + * + * @returns {void} + * @throws {Error} Endpoint unreachable. + */ + async function testEndpoint(endpoint) { + try { + const response = await fetch(endpoint) + if (response.ok) return + } catch (cause) { + alert(`Impossible de joindre l'adresse "${endpoint}"`) + throw new Error(`unable to connect to robot at ${endpoint}`, { + cause, + }) + } + } + + document.addEventListener('keydown', (event) => { + const key = event.keyCode + const { command, value } = keyMap[key] + const endpoint = getEndpoint() + sendCommand(endpoint, command, value) + }) + function storeInput() { ipInput = document.getElementById('ip').value valueRot = document.getElementById('valueRot').value