Files
klp-mono/apps/l3/public/websocket-test.html
砂糖 3db2ccf591 init
2025-10-10 16:47:38 +08:00

558 lines
22 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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;
}
.btn-warning {
background-color: #ffc107;
color: #212529;
}
.btn-secondary {
background-color: #6c757d;
color: white;
}
.btn-success {
background-color: #28a745;
color: white;
}
select {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
select:disabled {
background-color: #f8f9fa;
cursor: not-allowed;
}
</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>
<button id="btn_test_direct" class="btn btn-warning">直接测试接口</button>
</div>
<div class="control-panel">
<div class="input-group">
<label for="broadcast_message">广播消息:</label>
<input type="text" id="broadcast_message" placeholder="输入要广播的消息">
</div>
<div class="button-group">
<button id="btn_broadcast" class="btn btn-warning" disabled>发送广播</button>
</div>
</div>
<div class="control-panel">
<div class="input-group">
<label for="user_session">用户会话ID:</label>
<select id="user_session" disabled>
<option value="">请先获取在线用户</option>
</select>
</div>
<div class="input-group">
<label for="private_message">私信内容:</label>
<input type="text" id="private_message" placeholder="输入私信内容">
</div>
<div class="button-group">
<button id="btn_get_users" class="btn btn-secondary">获取在线用户</button>
<button id="btn_send_private" class="btn btn-success" disabled>发送私信</button>
</div>
</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');
var broadcastBtn = document.getElementById('btn_broadcast');
var getUsersBtn = document.getElementById('btn_get_users');
var sendPrivateBtn = document.getElementById('btn_send_private');
if (connected) {
statusDiv.textContent = '已连接';
statusDiv.className = 'status connected';
joinBtn.disabled = true;
exitBtn.disabled = false;
sendBtn.disabled = false;
broadcastBtn.disabled = false;
getUsersBtn.disabled = false;
sendPrivateBtn.disabled = false;
} else {
statusDiv.textContent = '未连接';
statusDiv.className = 'status disconnected';
joinBtn.disabled = false;
exitBtn.disabled = true;
sendBtn.disabled = true;
broadcastBtn.disabled = true;
getUsersBtn.disabled = true;
sendPrivateBtn.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 => {
addLog('服务器响应状态: ' + response.status + ' ' + response.statusText);
if (response.ok) {
return response.text().then(text => {
try {
return JSON.parse(text);
} catch (e) {
addLog('响应内容不是JSON格式: ' + text.substring(0, 200));
throw new Error('响应格式错误');
}
});
} else {
return response.text().then(text => {
addLog('错误响应内容: ' + text.substring(0, 200));
throw new Error('服务器响应错误: ' + response.status);
});
}
})
.then(data => {
addLog('服务器状态: ' + data.msg);
})
.catch(error => {
addLog('服务器测试失败: ' + error.message);
});
});
// 直接测试接口
$('#btn_test_direct').click(function() {
addLog('=== 直接测试接口开始 ===');
// 测试1: 直接访问接口
addLog('测试1: 直接访问 /websocket/test/status');
fetch('/websocket/test/status', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).then(response => {
addLog('响应状态: ' + response.status + ' ' + response.statusText);
addLog('响应头: ' + JSON.stringify(Object.fromEntries(response.headers.entries())));
return response.text();
}).then(text => {
addLog('响应内容: ' + text.substring(0, 500));
if (text.includes('<!DOCTYPE html>')) {
addLog('❌ 返回了HTML页面说明被重定向到登录页面');
} else {
addLog('✅ 返回了非HTML内容');
}
}).catch(error => {
addLog('请求失败: ' + error.message);
});
// 测试2: 测试简单控制器
setTimeout(() => {
addLog('测试2: 测试 /test/hello简单控制器');
fetch('/test/hello', {
method: 'GET'
}).then(response => {
addLog('简单控制器响应状态: ' + response.status + ' ' + response.statusText);
return response.text();
}).then(text => {
addLog('简单控制器响应内容: ' + text.substring(0, 200));
if (text.includes('<!DOCTYPE html>')) {
addLog('❌ 简单控制器也返回了HTML页面');
} else {
addLog('✅ 简单控制器返回了非HTML内容');
}
}).catch(error => {
addLog('简单控制器请求失败: ' + error.message);
});
}, 1000);
// 测试3: 测试actuator接口应该工作
setTimeout(() => {
addLog('测试3: 测试 /actuator/health应该工作');
fetch('/actuator/health', {
method: 'GET'
}).then(response => {
addLog('Actuator响应状态: ' + response.status + ' ' + response.statusText);
return response.text();
}).then(text => {
addLog('Actuator响应内容: ' + text.substring(0, 200));
}).catch(error => {
addLog('Actuator请求失败: ' + error.message);
});
}, 2000);
addLog('=== 直接测试接口结束 ===');
});
// 测试连接数
$('#btn_test_data').click(function() {
addLog('正在获取连接数...');
fetch('/websocket/test/connections', {
method: 'GET'
}).then(response => {
addLog('连接数响应状态: ' + response.status + ' ' + response.statusText);
if (response.ok) {
return response.text().then(text => {
try {
return JSON.parse(text);
} catch (e) {
addLog('连接数响应内容不是JSON格式: ' + text.substring(0, 200));
throw new Error('响应格式错误');
}
});
} else {
return response.text().then(text => {
addLog('连接数错误响应内容: ' + text.substring(0, 200));
throw new Error('获取连接数失败: ' + response.status);
});
}
}).then(data => {
addLog('当前连接数: ' + data.data);
}).catch(error => {
addLog('获取连接数失败: ' + error.message);
});
});
// 发送广播消息
$('#btn_broadcast').click(function() {
var message = $('#broadcast_message').val();
if (!message.trim()) {
addLog('请输入广播消息内容');
return;
}
addLog('正在发送广播消息: ' + message);
var formData = new FormData();
formData.append('message', message);
fetch('/websocket/test/broadcast', {
method: 'POST',
body: formData
}).then(response => {
addLog('广播响应状态: ' + response.status + ' ' + response.statusText);
if (response.ok) {
return response.text().then(text => {
try {
return JSON.parse(text);
} catch (e) {
addLog('广播响应内容不是JSON格式: ' + text.substring(0, 200));
throw new Error('响应格式错误');
}
});
} else {
return response.text().then(text => {
addLog('广播错误响应内容: ' + text.substring(0, 200));
throw new Error('广播发送失败: ' + response.status);
});
}
}).then(data => {
addLog('广播结果: ' + data.msg);
$('#broadcast_message').val('');
}).catch(error => {
addLog('广播发送失败: ' + error.message);
});
});
// 获取在线用户
$('#btn_get_users').click(function() {
addLog('正在获取在线用户...');
fetch('/websocket/test/users', {
method: 'GET'
}).then(response => {
addLog('用户列表响应状态: ' + response.status + ' ' + response.statusText);
if (response.ok) {
return response.text().then(text => {
try {
return JSON.parse(text);
} catch (e) {
addLog('用户列表响应内容不是JSON格式: ' + text.substring(0, 200));
throw new Error('响应格式错误');
}
});
} else {
return response.text().then(text => {
addLog('用户列表错误响应内容: ' + text.substring(0, 200));
throw new Error('获取用户列表失败: ' + response.status);
});
}
}).then(data => {
addLog('在线用户数量: ' + data.data.length);
updateUserSelect(data.data);
}).catch(error => {
addLog('获取用户列表失败: ' + error.message);
});
});
// 发送私信
$('#btn_send_private').click(function() {
var sessionId = $('#user_session').val();
var message = $('#private_message').val();
if (!sessionId) {
addLog('请选择要发送的用户');
return;
}
if (!message.trim()) {
addLog('请输入私信内容');
return;
}
addLog('正在发送私信给用户 ' + sessionId + ': ' + message);
var formData = new FormData();
formData.append('sessionId', sessionId);
formData.append('message', message);
fetch('/websocket/test/sendToUser', {
method: 'POST',
body: formData
}).then(response => {
addLog('私信响应状态: ' + response.status + ' ' + response.statusText);
if (response.ok) {
return response.text().then(text => {
try {
return JSON.parse(text);
} catch (e) {
addLog('私信响应内容不是JSON格式: ' + text.substring(0, 200));
throw new Error('响应格式错误');
}
});
} else {
return response.text().then(text => {
addLog('私信错误响应内容: ' + text.substring(0, 200));
throw new Error('私信发送失败: ' + response.status);
});
}
}).then(data => {
addLog('私信结果: ' + data.msg);
$('#private_message').val('');
}).catch(error => {
addLog('私信发送失败: ' + error.message);
});
});
// 更新用户选择下拉框
function updateUserSelect(users) {
var select = $('#user_session');
select.empty();
select.append('<option value="">请选择用户</option>');
users.forEach(function(user) {
select.append('<option value="' + user + '">' + user + '</option>');
});
select.prop('disabled', false);
addLog('用户列表已更新');
}
// 回车发送消息
$('#message').keypress(function(e) {
if (e.which == 13 && !e.shiftKey) {
e.preventDefault();
$('#btn_send').click();
}
});
});
</script>
</body>
</html>