-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
56 lines (49 loc) · 1.62 KB
/
index.html
File metadata and controls
56 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html>
<body>
<input id="in" placeholder="Type…">
<button id="btn">Send</button>
<pre id="out"></pre>
<script>
const Status = Object.freeze({
ACTIVE: 'active',
SHARING: 'sharing',
INACTIVE: 'inactive',
});
const log = msg => {
document.getElementById("out").textContent += msg + "\n";
};
// Use localhost (or 127.0.0.1), not 0.0.0.0
const ws = new WebSocket("ws://localhost:8235");
ws.addEventListener("message", e => {
let msg;
try {
msg = JSON.parse(e.data);
} catch {
console.error("Invalid JSON:", e.data);
return;
}
switch (msg.status) {
case Status.ACTIVE:
console.log("👉 Server says: ACTIVE");
break;
case Status.SHARING:
console.log("👉 Server says: SHARING");
break;
case Status.INACTIVE:
console.log("👉 Server says: INACTIVE");
break;
default:
console.warn("Unknown status:", msg.status);
}
});
document.getElementById("btn").onclick = () => {
const v = document.getElementById("in").value;
const r = v.trim() === "" ? Status.INACTIVE : Status.ACTIVE;
ws.send(JSON.stringify({ status: r }));
log("Sent: " + r);
document.getElementById("in").value = "";
};
</script>
</body>
</html>