PierreRouanet commited on
Commit
ec33ac6
·
1 Parent(s): d25dd0d
Files changed (3) hide show
  1. index.html +25 -21
  2. reachy-client.js +59 -35
  3. style.css +9 -0
index.html CHANGED
@@ -9,31 +9,35 @@
9
  </head>
10
 
11
  <body>
12
- <div class="container">
13
- <h1>Reachy Mini HTTP</h1>
14
- <div class="status" id="connection-status">
15
- <span class="dot disconnected" id="status-dot"></span>
16
- <span id="status-text">Disconnected</span>
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  </div>
18
- <form id="ip-form-http" style="margin-bottom: 20px;">
19
- <label for="ip-input-http">Reachy IP:</label>
20
- <input type="text" id="ip-input-http" name="ip" placeholder="Enter IP address" style="margin-left: 8px;">
21
- <button type="submit">Connect</button>
22
- </form>
23
- </div>
24
 
25
- <div class="container">
26
- <h1>Reachy Mini WS</h1>
27
- <div class="status" id="connection-status-ws">
28
- <span class="dot disconnected" id="status-dot-ws"></span>
29
- <span id="status-text-ws">Disconnected</span>
 
30
  </div>
31
- <form id="ip-form-ws" style="margin-bottom: 20px;">
32
- <label for="ip-input-ws">Reachy IP:</label>
33
- <input type="text" id="ip-input-ws" name="ip" placeholder="Enter IP address" style="margin-left: 8px;">
34
- <button type="submit">Connect</button>
35
- </form>
36
  </div>
 
37
  <script src="reachy-client.js"></script>
38
  </body>
39
 
 
9
  </head>
10
 
11
  <body>
12
+ <div class="flex-row justify-center align-center" style="margin-top: 32px;">
13
+ <h3>Start Reachy Mini daemon before testing connections!</h3>
14
+ <div class="flex-col-2">
15
+ <div class="container">
16
+ <h1>Reachy Mini HTTP</h1>
17
+ <div class="status" id="connection-status">
18
+ <span class="dot disconnected" id="status-dot-http"></span>
19
+ <span id="status-text-http">Disconnected</span>
20
+ </div>
21
+ </div>
22
+
23
+ <div class="container">
24
+ <h1>Reachy Mini WS</h1>
25
+ <div class="status" id="connection-status-ws">
26
+ <span class="dot disconnected" id="status-dot-ws"></span>
27
+ <span id="status-text-ws">Disconnected</span>
28
+ </div>
29
+ </div>
30
  </div>
 
 
 
 
 
 
31
 
32
+ <div class="container align-center">
33
+ <form id="ip-form">
34
+ <label for="ip-input">Reachy IP:</label>
35
+ <input type="text" id="ip-input" name="ip" placeholder="localhost" style="margin-left: 8px;">
36
+ <button type="submit" onclick="event.preventDefault(); tryConnect();">Connect</button>
37
+ </form>
38
  </div>
 
 
 
 
 
39
  </div>
40
+
41
  <script src="reachy-client.js"></script>
42
  </body>
43
 
reachy-client.js CHANGED
@@ -1,51 +1,75 @@
1
- let serverIp = 'localhost';
 
2
 
3
- function setServerIp(ip) {
4
- serverIp = ip;
5
- reachyClient.reachyServer = `${serverIp}:8000`;
6
- }
7
 
8
- document.addEventListener('DOMContentLoaded', () => {
9
- const form = document.getElementById('server-ip-form');
10
- const input = document.getElementById('server-ip-input');
11
- if (form && input) {
12
- form.addEventListener('submit', (e) => {
13
- e.preventDefault();
14
- setServerIp(input.value);
15
- });
16
- }
17
- });
18
 
19
  const reachyClient = {
20
- getStatus: async () => {
21
- const response = await fetch(`http://${reachyClient.reachyServer}/api/daemon/status`, { mode: 'cors' });
22
- return response.json();
23
- },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
 
 
 
 
25
  getWsStatus: async () => {
26
- const ws = new WebSocket(`ws://${reachyClient.reachyServer}/api/state/ws/full`);
27
- ws.onopen = () => {
28
- const statusDiv = document.getElementById('status-text-ws');
29
- statusDiv.innerText = 'Connected via WebSocket';
30
- ws.close();
31
- };
32
- ws.onerror = () => {
33
- const statusDiv = document.getElementById('status-text-ws');
 
 
 
 
 
 
 
 
 
 
 
 
34
  statusDiv.innerText = 'WebSocket Connection Error';
35
- };
 
 
 
36
  },
37
  };
38
 
 
 
 
39
 
40
- document.addEventListener('DOMContentLoaded', async () => {
41
- const status = await reachyClient.getStatus();
42
-
43
- const statusDiv = document.getElementById('status-text');
44
- statusDiv.innerText = status.state;
45
- });
46
 
47
- document.addEventListener('DOMContentLoaded', async () => {
48
  await reachyClient.getWsStatus();
 
 
 
 
49
  });
50
 
51
 
 
 
1
+ let reachyIp = 'localhost';
2
+ let reachyPort = 8000;
3
 
 
 
 
 
4
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  const reachyClient = {
7
+ getHTTPStatus: async () => {
8
+ const statusDiv = document.getElementById('status-text-http');
9
+ const statusDot = document.getElementById('status-dot-http');
10
+
11
+ try {
12
+ const resp = await fetch(`http://${reachyIp}:${reachyPort}/api/daemon/status`, { mode: 'cors' });
13
+ if (resp.ok) {
14
+ const status = await resp.json();
15
+ statusDiv.innerText = `Connected: ${status.state}`;
16
+ statusDot.classList.remove('disconnected');
17
+ statusDot.classList.add('connected');
18
+ console.log("HTTP OK");
19
+ } else {
20
+ statusDiv.innerText = 'HTTP Connection Error';
21
+ statusDot.classList.remove('connected');
22
+ statusDot.classList.add('disconnected');
23
+ console.log("HTTP ERROR");
24
+ }
25
 
26
+ } catch (error) {
27
+ statusDiv.innerText = 'HTTP Connection Error';
28
+ console.log("HTTP ERROR");
29
+ }
30
+ },
31
  getWsStatus: async () => {
32
+ const statusDiv = document.getElementById('status-text-ws');
33
+ const statusDot = document.getElementById('status-dot-ws');
34
+
35
+ try {
36
+
37
+ const ws = new WebSocket(`ws://localhost:8000/api/state/ws/full`);
38
+
39
+ ws.onmessage = (msg) => {
40
+ statusDiv.innerText = 'Connected: running';
41
+ statusDot.classList.remove('disconnected');
42
+ statusDot.classList.add('connected');
43
+ ws.close();
44
+ console.log("WS OK");
45
+ };
46
+ ws.onerror = () => {
47
+ const statusDiv = document.getElementById('status-text-ws');
48
+ statusDiv.innerText = 'WebSocket Connection Error';
49
+ console.log("WS ERROR");
50
+ };
51
+ } catch (error) {
52
  statusDiv.innerText = 'WebSocket Connection Error';
53
+ statusDot.classList.remove('connected');
54
+ statusDot.classList.add('disconnected');
55
+ console.log("WS ERROR");
56
+ }
57
  },
58
  };
59
 
60
+ const tryConnect = async () => {
61
+ const ipInput = document.getElementById('ip-input');
62
+ reachyIp = ipInput.value || 'localhost';
63
 
64
+ console.log(`Trying to connect to Reachy at ${reachyIp}...`);
 
 
 
 
 
65
 
66
+ await reachyClient.getHTTPStatus();
67
  await reachyClient.getWsStatus();
68
+ };
69
+
70
+ document.addEventListener('DOMContentLoaded', () => {
71
+ tryConnect();
72
  });
73
 
74
 
75
+
style.css CHANGED
@@ -1,3 +1,12 @@
 
 
 
 
 
 
 
 
 
1
  body {
2
  background: #f6f8fa;
3
  font-family: 'Segoe UI', Arial, sans-serif;
 
1
+ /* Flex row for two containers side by side */
2
+ .flex-col-2 {
3
+ display: flex;
4
+ flex-direction: row;
5
+ gap: 32px;
6
+ justify-content: center;
7
+ align-items: flex-start;
8
+ margin-bottom: 32px;
9
+ }
10
  body {
11
  background: #f6f8fa;
12
  font-family: 'Segoe UI', Arial, sans-serif;