-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
45 lines (44 loc) · 1.52 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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket demo</title>
</head>
<body>
<input placeholder="Your name here"></input>
<button onclick="sendMessage()">Send message</button>
<br />
<span>Message from server: </span>
<span id="response"></span>
<script>
function sendMessage() {
let ws = new WebSocket("ws://localhost:8765/")
let test = 'hello'
ws.onopen = function (event) {
ws.send(document.getElementsByTagName('input')[0].value);
}
ws.onerror = function() {
console.error("error");
}
ws.onclose = function() {
console.log("closed");
}
ws.onmessage = function (event) {
let server_resp = event.data;
document.getElementById('response').textContent= server_resp;
}
}
</script>
<script>
let ws_time = new WebSocket("ws://localhost:5678/"),
messages = document.createElement('ul');
ws_time.onmessage = function (event) {
let messages = document.getElementsByTagName('ul')[0],
message = document.createElement('li'),
content = document.createTextNode(event.data);
message.appendChild(content);
messages.appendChild(message);
};
document.body.appendChild(messages);
</script>
</body>
</html>