-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsockets.html
More file actions
32 lines (29 loc) · 800 Bytes
/
websockets.html
File metadata and controls
32 lines (29 loc) · 800 Bytes
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
<!doctype html>
<html lang="en">
<head></head>
<body>
<input id="input" required type="text" />
<button onclick="send()">Send</button>
<pre id="output">
{{range .Messages}}
<div>Server: {{.Text}}</div>
{{end}}
</pre>
<script>
const input = document.getElementById("input");
const output = document.getElementById("output");
const socket = new WebSocket("ws://localhost:{{.Port}}/messages");
socket.onopen = function (e) {
console.log(e)
output.innerHTML = "<div><b>Status: Connected\n</b></div>" + output.innerHTML;
};
socket.onmessage = function (e) {
output.innerHTML += "<div>Server: " + e.data + "\n</div>";
};
function send() {
socket.send(input.value);
input.value = "";
}
</script>
</body>
</html>