项目初始化
This commit is contained in:
45
src/main/resources/static/websocket.html
Normal file
45
src/main/resources/static/websocket.html
Normal file
@@ -0,0 +1,45 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>WebSocket 客户端</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>WebSocket 消息展示</h2>
|
||||
<div id="msgBox" style="border:1px solid #ccc; padding:10px; width:400px; height:200px; overflow:auto;"></div>
|
||||
|
||||
<script>
|
||||
let socket;
|
||||
let sessionId = ""; // 服务端推送时会返回当前连接的 sessionId
|
||||
|
||||
// 建立 WebSocket 连接
|
||||
function connect() {
|
||||
socket = new WebSocket("ws://192.168.1.5:8081/ws/socket");
|
||||
socket.onopen = function () {
|
||||
log("WebSocket 已连接");
|
||||
};
|
||||
|
||||
socket.onmessage = function (event) {
|
||||
log("收到消息:" + event.data);
|
||||
};
|
||||
|
||||
socket.onclose = function () {
|
||||
log("连接已关闭");
|
||||
};
|
||||
|
||||
socket.onerror = function (error) {
|
||||
log("发生错误:" + error);
|
||||
};
|
||||
}
|
||||
|
||||
function log(msg) {
|
||||
let box = document.getElementById("msgBox");
|
||||
box.innerHTML += "<p>" + msg + "</p>";
|
||||
box.scrollTop = box.scrollHeight;
|
||||
}
|
||||
|
||||
// 页面加载完成后连接
|
||||
window.onload = connect;
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user