reachy-mini-web-api-test / reachy-client.js
PierreRouanet's picture
Update reachy-client.js
6b27ded
let reachyIp = 'localhost';
let reachyPort = 8000;
const reachyClient = {
getHTTPStatus: async () => {
const statusDiv = document.getElementById('status-text-http');
const statusDot = document.getElementById('status-dot-http');
try {
const resp = await fetch(`http://${reachyIp}:${reachyPort}/api/daemon/status`, { mode: 'cors' });
if (resp.ok) {
const status = await resp.json();
statusDiv.innerText = `Connected: ${status.state}`;
statusDot.classList.remove('disconnected');
statusDot.classList.add('connected');
console.log("HTTP OK");
} else {
statusDiv.innerText = 'HTTP Connection Error';
statusDot.classList.remove('connected');
statusDot.classList.add('disconnected');
console.log("HTTP ERROR");
}
} catch (error) {
statusDiv.innerText = 'HTTP Connection Error';
console.log("HTTP ERROR");
}
},
getWsStatus: async () => {
const statusDiv = document.getElementById('status-text-ws');
const statusDot = document.getElementById('status-dot-ws');
try {
const ws = new WebSocket(`ws://${reachyIp}:${reachyPort}/api/state/ws/full`);
ws.onmessage = (msg) => {
statusDiv.innerText = 'Connected: running';
statusDot.classList.remove('disconnected');
statusDot.classList.add('connected');
ws.close();
console.log("WS OK");
};
ws.onerror = () => {
const statusDiv = document.getElementById('status-text-ws');
statusDiv.innerText = 'WebSocket Connection Error';
console.log("WS ERROR");
};
} catch (error) {
statusDiv.innerText = 'WebSocket Connection Error';
statusDot.classList.remove('connected');
statusDot.classList.add('disconnected');
console.log("WS ERROR");
}
},
};
const tryConnect = async () => {
const ipInput = document.getElementById('ip-input');
reachyIp = ipInput.value || 'localhost';
console.log(`Trying to connect to Reachy at ${reachyIp}...`);
await reachyClient.getHTTPStatus();
await reachyClient.getWsStatus();
};
document.addEventListener('DOMContentLoaded', () => {
tryConnect();
});