-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
example.html
46 lines (41 loc) · 1.39 KB
/
example.html
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
<!DOCTYPE html>
<html>
<!-- Example program to receive data from input overlay via the websocket server
This can be used to visualize inputs through css or html5 canvas methods
instead of the builtin overlay source.
-->
<head>
<meta charset="utf-8" />
<title>input-overlay websocket example</title>
</head>
<body>
<ul id="log" style="list-style-type: none"></ul>
</body>
<script>
var l = document.getElementById("log");
var require_clear = false;
var count = 0;
function on_data(e) {
console.log(e.data);
l.insertAdjacentHTML("beforeend", `<li>${e.data}</li>`);
// Only keep last 10 entries
if (!require_clear) {
count++;
if (count > 10) require_clear = true;
} else {
l.children[0].remove();
}
}
function start_websocket() {
var ws = new WebSocket("ws://localhost:16899/");
ws.onmessage = on_data;
ws.onerror = (e) => console.log('WebSocket error: ' + e);
ws.onclose = () => {
// connection closed, discard old websocket and create a new one in 2 seconds
ws = null;
setTimeout(start_websocket, 2000);
};
}
start_websocket();
</script>
</html>