Create public/index.html
Browse files- public/index.html +53 -0
public/index.html
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>WebSocket 测试</title>
|
| 5 |
+
<meta charset="utf-8" />
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>WebSocket 客户端</h1>
|
| 9 |
+
<div id="status">正在连接...</div>
|
| 10 |
+
<div
|
| 11 |
+
id="messages"
|
| 12 |
+
style="
|
| 13 |
+
border: 1px solid #ccc;
|
| 14 |
+
height: 200px;
|
| 15 |
+
overflow-y: scroll;
|
| 16 |
+
margin-top: 10px;
|
| 17 |
+
padding: 10px;
|
| 18 |
+
"
|
| 19 |
+
></div>
|
| 20 |
+
|
| 21 |
+
<script>
|
| 22 |
+
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
| 23 |
+
const socketUrl = protocol + "//" + window.location.host;
|
| 24 |
+
|
| 25 |
+
const socket = new WebSocket(socketUrl);
|
| 26 |
+
const statusDiv = document.getElementById("status");
|
| 27 |
+
const msgDiv = document.getElementById("messages");
|
| 28 |
+
|
| 29 |
+
function log(msg) {
|
| 30 |
+
msgDiv.innerHTML += "<p>" + msg + "</p>";
|
| 31 |
+
msgDiv.scrollTop = msgDiv.scrollHeight;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
socket.onopen = () => {
|
| 35 |
+
statusDiv.innerText = "状态:已连接";
|
| 36 |
+
statusDiv.style.color = "green";
|
| 37 |
+
log("系统:连接已建立");
|
| 38 |
+
|
| 39 |
+
socket.send("hello");
|
| 40 |
+
log("发送:hello");
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
socket.onmessage = (event) => {
|
| 44 |
+
log("收到来自服务器:" + event.data);
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
socket.onclose = () => {
|
| 48 |
+
statusDiv.innerText = "状态:连接已断开";
|
| 49 |
+
statusDiv.style.color = "red";
|
| 50 |
+
};
|
| 51 |
+
</script>
|
| 52 |
+
</body>
|
| 53 |
+
</html>
|