feat(client): add function to test connection on ip update

This commit is contained in:
Julien Oculi 2024-06-25 12:58:11 +02:00
parent 994254a1a5
commit 6044a3e50c

View file

@ -116,7 +116,7 @@
document document
.querySelector('#settings') .querySelector('#settings')
.addEventListener('submit', (event) => { .addEventListener('submit', async (event) => {
// Don't interrupt event if not form submitting // Don't interrupt event if not form submitting
if (!(event instanceof SubmitEvent)) return true if (!(event instanceof SubmitEvent)) return true
if (event.target === null) return true if (event.target === null) return true
@ -128,7 +128,36 @@
if (ipAddress === null) return 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() { function storeInput() {