PierreRouanet commited on
Commit
d25dd0d
·
verified ·
1 Parent(s): 3557ec6

Create reachy-client.js

Browse files
Files changed (1) hide show
  1. reachy-client.js +51 -0
reachy-client.js ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+