-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.html
62 lines (56 loc) · 1.6 KB
/
connection.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>connection</title>
<style>
#connection {
text-align: center;
padding: 20px;
}
.allitems {
text-align: center;
margin-top: 50px;
}
.allitems div {
width: 60%;
padding: 20px;
margin-bottom: 20px;
border: 1px solid;
text-align: center;
display: inline-block;
}
</style>
</head>
<body>
<div id="connection">connection...</div>
<div class="allitems">
<div>This is the first module!</div>
<div>This is the second module!</div>
<div>This is the third modele!</div>
</div>
<script>
var mess = document.getElementById("connection");
if (window.WebSocket) {
var ws = new WebSocket('ws://localhost:8001');
ws.onopen = function (e) {
console.log("Connect the server to success");
ws.send("connection");
}
ws.onclose = function (e) {
console.log("Close pages");
}
ws.onerror = function (e) {
console.log("This is a error");
}
ws.onmessage = function (e) {
mess.innerHTML = "Connection success!"
document.querySelector(".allitems").onclick = function (e) {
var time = new Date();
ws.send(time + "click“" + e.target.innerHTML + "”");
}
}
}
</script>
</body>
</html>