-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
64 lines (54 loc) · 1.6 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
<head>
<title>Go WebSocket Test</title>
<script>
function log(message) {
var logElement = document.getElementById("log");
logElement.innerText += new Date().toISOString() + ": " + message + "\n";
}
function getWsUrl() {
var loc = window.location, new_uri;
if (loc.protocol === "https:") {
new_uri = "wss:";
} else {
new_uri = "ws:";
}
new_uri += "//" + loc.host + "/websocket";
return new_uri;
}
var ws;
function connect() {
log("Connecting");
ws = new WebSocket(getWsUrl());
ws.onopen = () => {
log("Connected");
};
ws.onmessage = (event) => {
log("Received: " + event.data);
};
ws.onclose = (event) => {
log("Connection Closed, Reconnecting");
setTimeout(connect, 1000);
};
ws.onerror = (error) => {
log("Error, Closing");
ws.close();
};
}
window.onload = (event) => {
// Connect button and input to send messages
document.getElementById('send').onclick = (event) => {
ws.send(document.getElementById("payload").value);
}
// Start a connection
connect();
};
</script>
</head>
<body>
<input id="payload" type="text" />
<button id="send">Send</button>
<pre id="log"></pre>
</body>
</html>