Files
AUTO-MAS-test/websocket_force_test.html
2025-10-02 15:51:32 +08:00

254 lines
8.6 KiB
HTML
Raw Permalink 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="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket强制连接测试</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
button {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
margin: 5px;
}
button:hover {
background: #0056b3;
}
.force-btn {
background: #dc3545;
}
.force-btn:hover {
background: #c82333;
}
.status {
margin: 10px 0;
padding: 10px;
border-radius: 5px;
}
.success {
background: #d4edda;
color: #155724;
}
.error {
background: #f8d7da;
color: #721c24;
}
.warning {
background: #fff3cd;
color: #856404;
}
.log {
background: #f8f9fa;
border: 1px solid #dee2e6;
padding: 10px;
margin-top: 10px;
max-height: 400px;
overflow-y: auto;
font-family: monospace;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<h1>WebSocket强制连接测试工具</h1>
<div class="status" id="status">状态:未知</div>
<div>
<button onclick="testNormalConnect()">测试正常连接</button>
<button onclick="testForceConnect()" class="force-btn">测试强制连接</button>
<button onclick="testDirectConnect()">测试直接WebSocket连接</button>
<button onclick="checkStatus()">检查连接状态</button>
<button onclick="clearLog()">清空日志</button>
</div>
<div class="log" id="log"></div>
</div>
<script>
let ws = null;
function log(message, type = 'info') {
const logDiv = document.getElementById('log');
const timestamp = new Date().toLocaleTimeString();
const className = type === 'error' ? 'color: red;' :
type === 'success' ? 'color: green;' :
type === 'warning' ? 'color: orange;' : '';
logDiv.innerHTML += `<div style="${className}">[${timestamp}] ${message}</div>`;
logDiv.scrollTop = logDiv.scrollHeight;
}
function updateStatus(message, type = 'info') {
const statusDiv = document.getElementById('status');
statusDiv.textContent = `状态:${message}`;
statusDiv.className = `status ${type}`;
}
// 测试正常连接(通过前端框架)
async function testNormalConnect() {
log('🔍 测试正常连接模式...');
updateStatus('测试正常连接中...', 'warning');
try {
// 这里需要调用前端框架的连接方法
if (window.wsDebug && window.wsDebug.normalConnect) {
const result = await window.wsDebug.normalConnect();
if (result) {
log('✅ 正常连接成功', 'success');
updateStatus('正常连接成功', 'success');
} else {
log('❌ 正常连接失败', 'error');
updateStatus('正常连接失败', 'error');
}
} else {
log('⚠️ wsDebug.normalConnect 不可用', 'warning');
updateStatus('调试功能不可用', 'warning');
}
} catch (error) {
log(`❌ 正常连接异常: ${error}`, 'error');
updateStatus('正常连接异常', 'error');
}
}
// 测试强制连接(通过前端框架)
async function testForceConnect() {
log('🚀 测试强制连接模式...');
updateStatus('测试强制连接中...', 'warning');
try {
if (window.wsDebug && window.wsDebug.forceConnect) {
const result = await window.wsDebug.forceConnect();
if (result) {
log('✅ 强制连接成功', 'success');
updateStatus('强制连接成功', 'success');
} else {
log('❌ 强制连接失败', 'error');
updateStatus('强制连接失败', 'error');
}
} else {
log('⚠️ wsDebug.forceConnect 不可用', 'warning');
updateStatus('调试功能不可用', 'warning');
}
} catch (error) {
log(`❌ 强制连接异常: ${error}`, 'error');
updateStatus('强制连接异常', 'error');
}
}
// 测试直接WebSocket连接
function testDirectConnect() {
log('🔗 测试直接WebSocket连接...');
updateStatus('测试直接连接中...', 'warning');
try {
if (ws && ws.readyState === WebSocket.OPEN) {
log('⚠️ WebSocket已经连接先关闭', 'warning');
ws.close();
}
ws = new WebSocket('ws://localhost:36163/api/core/ws');
ws.onopen = () => {
log('✅ 直接WebSocket连接成功', 'success');
updateStatus('直接连接成功', 'success');
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
log(`📨 收到消息: ${JSON.stringify(data)}`, 'info');
} catch (e) {
log(`📨 收到消息: ${event.data}`, 'info');
}
};
ws.onerror = (error) => {
log(`❌ WebSocket错误: ${error}`, 'error');
updateStatus('直接连接错误', 'error');
};
ws.onclose = (event) => {
log(`🔒 WebSocket关闭: code=${event.code}, reason="${event.reason}"`, 'warning');
updateStatus('连接已关闭', 'warning');
};
} catch (error) {
log(`❌ 直接连接异常: ${error}`, 'error');
updateStatus('直接连接异常', 'error');
}
}
// 检查连接状态
function checkStatus() {
log('🔍 检查连接状态...');
// 检查直接WebSocket
if (ws) {
const states = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
log(`📡 直接WebSocket状态: ${states[ws.readyState]} (${ws.readyState})`);
} else {
log('📡 直接WebSocket: 未创建');
}
// 检查框架WebSocket
if (window.wsDebug) {
try {
const info = window.wsDebug.getConnectionInfo ? window.wsDebug.getConnectionInfo() : {};
log(`🔧 框架WebSocket状态: ${JSON.stringify(info, null, 2)}`);
const storage = window.wsDebug.getGlobalStorage ? window.wsDebug.getGlobalStorage() : {};
log(`💾 全局存储状态: allowNewConnection=${storage.allowNewConnection}, connectionReason="${storage.connectionReason}"`);
if (window.wsDebug.allowedReasons) {
log(`✅ 允许的连接原因: ${JSON.stringify(window.wsDebug.allowedReasons)}`);
}
} catch (error) {
log(`❌ 检查框架状态异常: ${error}`, 'error');
}
} else {
log('🔧 框架WebSocket调试功能不可用');
}
}
function clearLog() {
document.getElementById('log').innerHTML = '';
}
// 页面加载时检查状态
window.addEventListener('load', () => {
log('📋 WebSocket强制连接测试工具已加载');
checkStatus();
});
</script>
</body>
</html>