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