-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
46 lines (42 loc) · 1.25 KB
/
index.html
File metadata and controls
46 lines (42 loc) · 1.25 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
<!doctype html>
<html lang="en">
<head>
<title>WebSocket</title>
<script>
let echoService;
append = function(text) {
document.getElementById("websocket_events").insertAdjacentHTML('beforeend',
"<li>" + text + "</li>",
);
};
window.onload = function() {
let host = window.location.host;
echoService = new WebSocket('ws://' + host + ':8081');
echoService.onmessage = function(event) {
append(event.data);
};
echoService.onopen = function() {
append("Connected to WebSocket!");
};
echoService.onclose = function() {
append("Connection closed");
};
echoService.onerror = function() {
append("Error happens");
};
};
function sendMessage() {
let message = document.getElementById("message").value;
echoService.send(message);
}
</script>
</head>
<body>
<div>
Message: <input value="Hello!" type="text" id="message"/><br><br>
<input type="button" value="Submit" onclick="sendMessage();"/><br>
<ul id="websocket_events">
</ul>
</div>
</body>
</html>