-
Notifications
You must be signed in to change notification settings - Fork 195
/
index.html
34 lines (32 loc) · 909 Bytes
/
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket第一个案例</title>
</head>
<body>
<h1>Echo Test</h1>
<input type="text" id="sendTxt" />
<button id="sendBtn">发送</button>
<div id="recv">
</div>
<script>
var websocket = new WebSocket("wss://echo.websocket.org/");
websocket.onopen = function () {
console.log("websocket open");
document.getElementById("recv").innerHTML = "Connected";
}
websocket.onclose = function () {
console.log("websocket close");
}
websocket.onmessage = function (e) {
console.log(e.data);
document.getElementById("recv").innerHTML = e.data;
}
document.getElementById("sendBtn").onclick = function () {
var txt = document.getElementById("sendTxt").value;
websocket.send(txt);
}
</script>
</body>
</html>