Files
klp-oa/klp-ui/public/websocket-test.html

257 lines
8.6 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>WebSocket测试界面</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" ></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.control-panel {
margin-bottom: 20px;
padding: 15px;
background: #f8f9fa;
border-radius: 5px;
}
.input-group {
margin-bottom: 10px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.input-group input {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.button-group {
margin-top: 10px;
}
.btn {
padding: 8px 16px;
margin-right: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.btn-primary {
background-color: #007bff;
color: white;
}
.btn-success {
background-color: #28a745;
color: white;
}
.btn-danger {
background-color: #dc3545;
color: white;
}
.btn:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.status {
padding: 10px;
margin: 10px 0;
border-radius: 4px;
font-weight: bold;
}
.status.connected {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status.disconnected {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.log-area {
background: #f8f9fa;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
height: 400px;
overflow-y: auto;
font-family: 'Courier New', monospace;
font-size: 12px;
white-space: pre-wrap;
}
.back-btn {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="back-btn">
<button class="btn btn-primary" onclick="window.location.href='/'">返回首页</button>
</div>
<h1>WebSocket实时通信测试</h1>
<div class="control-panel">
<div class="input-group">
<label for="url">WebSocket连接地址:</label>
<input type="text" id="url" value="ws://127.0.0.1:8080/websocket/message" placeholder="输入WebSocket地址">
</div>
<div class="button-group">
<button id="btn_join" class="btn btn-primary">连接</button>
<button id="btn_exit" class="btn btn-danger" disabled>断开</button>
<button id="btn_test_server" class="btn btn-info">测试服务器</button>
</div>
<div id="status" class="status disconnected">
未连接
</div>
</div>
<div class="control-panel">
<div class="input-group">
<label for="message">消息内容:</label>
<textarea id="message" rows="3" placeholder="输入要发送的消息"></textarea>
</div>
<div class="button-group">
<button id="btn_send" class="btn btn-success" disabled>发送消息</button>
</div>
</div>
<div class="input-group">
<label for="text_content">消息日志:</label>
<div id="text_content" class="log-area"></div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
var ws = null;
// 更新状态显示
function updateStatus(connected) {
var statusDiv = document.getElementById('status');
var joinBtn = document.getElementById('btn_join');
var exitBtn = document.getElementById('btn_exit');
var sendBtn = document.getElementById('btn_send');
if (connected) {
statusDiv.textContent = '已连接';
statusDiv.className = 'status connected';
joinBtn.disabled = true;
exitBtn.disabled = false;
sendBtn.disabled = false;
} else {
statusDiv.textContent = '未连接';
statusDiv.className = 'status disconnected';
joinBtn.disabled = false;
exitBtn.disabled = true;
sendBtn.disabled = true;
}
}
// 添加日志
function addLog(message) {
var logArea = document.getElementById('text_content');
var timestamp = new Date().toLocaleTimeString();
logArea.textContent += '[' + timestamp + '] ' + message + '\n';
logArea.scrollTop = logArea.scrollHeight;
}
// 连接
$('#btn_join').click(function() {
var url = $("#url").val();
addLog('正在连接: ' + url);
try {
ws = new WebSocket(url);
ws.onopen = function(event) {
addLog('WebSocket连接成功!');
updateStatus(true);
}
ws.onmessage = function(event) {
addLog('收到消息: ' + event.data);
}
ws.onclose = function(event) {
addLog('WebSocket连接关闭! 代码: ' + event.code + ', 原因: ' + event.reason);
updateStatus(false);
}
ws.onerror = function(error) {
addLog('WebSocket连接错误: ' + JSON.stringify(error));
updateStatus(false);
}
} catch (error) {
addLog('WebSocket连接异常: ' + error.message);
updateStatus(false);
}
});
// 发送消息
$('#btn_send').click(function() {
var message = $('#message').val();
if (ws && ws.readyState === WebSocket.OPEN && message.trim()) {
ws.send(message);
addLog('发送消息: ' + message);
$('#message').val('');
} else {
addLog('请先连接WebSocket或输入消息内容');
}
});
// 断开
$('#btn_exit').click(function() {
if (ws) {
ws.close();
ws = null;
updateStatus(false);
}
});
// 测试服务器状态
$('#btn_test_server').click(function() {
addLog('正在测试服务器状态...');
fetch('/websocket/test/status')
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('服务器响应错误: ' + response.status);
}
})
.then(data => {
addLog('服务器状态: ' + data.msg);
})
.catch(error => {
addLog('服务器测试失败: ' + error.message);
});
});
// 回车发送消息
$('#message').keypress(function(e) {
if (e.which == 13 && !e.shiftKey) {
e.preventDefault();
$('#btn_send').click();
}
});
});
</script>
</body>
</html>