Files
vf_react/test_websocket.html

290 lines
9.6 KiB
HTML

<!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>
<script src="https://cdn.socket.io/4.7.4/socket.io.min.js"></script>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 1000px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
border-radius: 8px;
padding: 30px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
border-bottom: 3px solid #4CAF50;
padding-bottom: 10px;
}
.status {
padding: 15px;
margin: 20px 0;
border-radius: 5px;
font-weight: bold;
}
.status.connected {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status.disconnected {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
button {
background: #4CAF50;
color: white;
border: none;
padding: 12px 24px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
margin: 5px;
}
button:hover {
background: #45a049;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.log {
background: #f9f9f9;
border: 1px solid #ddd;
border-radius: 5px;
padding: 15px;
margin-top: 20px;
max-height: 400px;
overflow-y: auto;
font-family: 'Courier New', monospace;
font-size: 13px;
}
.log-item {
padding: 8px;
margin: 5px 0;
border-left: 3px solid #4CAF50;
background: white;
}
.log-item.error {
border-left-color: #f44336;
background: #ffebee;
}
.log-item.event {
border-left-color: #2196F3;
background: #e3f2fd;
}
.event-card {
background: #fff3cd;
border: 2px solid #ffc107;
border-radius: 8px;
padding: 15px;
margin: 10px 0;
}
.event-card h3 {
margin-top: 0;
color: #856404;
}
.timestamp {
color: #666;
font-size: 12px;
}
.controls {
margin: 20px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>🔌 WebSocket 事件推送测试页面</h1>
<div id="status" class="status disconnected">
状态: 未连接
</div>
<div class="controls">
<button id="connectBtn" onclick="connect()">连接 WebSocket</button>
<button id="subscribeBtn" onclick="subscribe()" disabled>订阅所有事件</button>
<button id="unsubscribeBtn" onclick="unsubscribe()" disabled>取消订阅</button>
<button id="disconnectBtn" onclick="disconnect()" disabled>断开连接</button>
<button onclick="clearLog()">清空日志</button>
</div>
<div id="events"></div>
<h2>📋 日志</h2>
<div id="log" class="log"></div>
</div>
<script>
let socket = null;
let isSubscribed = false;
function updateStatus(connected) {
const statusDiv = document.getElementById('status');
const connectBtn = document.getElementById('connectBtn');
const subscribeBtn = document.getElementById('subscribeBtn');
const unsubscribeBtn = document.getElementById('unsubscribeBtn');
const disconnectBtn = document.getElementById('disconnectBtn');
if (connected) {
statusDiv.className = 'status connected';
statusDiv.textContent = '状态: 已连接 ✓';
connectBtn.disabled = true;
subscribeBtn.disabled = false;
disconnectBtn.disabled = false;
} else {
statusDiv.className = 'status disconnected';
statusDiv.textContent = '状态: 未连接 ✗';
connectBtn.disabled = false;
subscribeBtn.disabled = true;
unsubscribeBtn.disabled = true;
disconnectBtn.disabled = true;
isSubscribed = false;
}
}
function log(message, type = 'info') {
const logDiv = document.getElementById('log');
const timestamp = new Date().toLocaleTimeString();
const item = document.createElement('div');
item.className = `log-item ${type}`;
item.innerHTML = `<span class="timestamp">[${timestamp}]</span> ${message}`;
logDiv.insertBefore(item, logDiv.firstChild);
console.log(`[${timestamp}] ${message}`);
}
function showEvent(event) {
const eventsDiv = document.getElementById('events');
const card = document.createElement('div');
card.className = 'event-card';
card.innerHTML = `
<h3>🔔 新事件通知</h3>
<p><strong>ID:</strong> ${event.id}</p>
<p><strong>标题:</strong> ${event.title}</p>
<p><strong>类型:</strong> ${event.event_type || 'N/A'}</p>
<p><strong>重要性:</strong> ${event.importance || 'N/A'}</p>
<p><strong>描述:</strong> ${event.description || 'N/A'}</p>
<p><strong>热度:</strong> ${event.hot_score || 0}</p>
<p class="timestamp">收到时间: ${new Date().toLocaleString()}</p>
`;
eventsDiv.insertBefore(card, eventsDiv.firstChild);
// 只保留最近 5 个事件
while (eventsDiv.children.length > 5) {
eventsDiv.removeChild(eventsDiv.lastChild);
}
}
function connect() {
log('正在连接到 WebSocket 服务器...');
socket = io('http://localhost:5001', {
transports: ['websocket', 'polling'],
reconnection: true,
reconnectionAttempts: 5,
reconnectionDelay: 1000
});
socket.on('connect', () => {
log('✅ WebSocket 连接成功!', 'info');
updateStatus(true);
});
socket.on('connection_response', (data) => {
log(`📡 服务器响应: ${JSON.stringify(data)}`, 'info');
});
socket.on('new_event', (eventData) => {
log(`🔔 收到新事件: ${eventData.title}`, 'event');
showEvent(eventData);
});
socket.on('subscription_confirmed', (data) => {
log(`✅ 订阅成功: ${JSON.stringify(data)}`, 'info');
isSubscribed = true;
document.getElementById('subscribeBtn').disabled = true;
document.getElementById('unsubscribeBtn').disabled = false;
});
socket.on('unsubscription_confirmed', (data) => {
log(`✅ 取消订阅成功: ${JSON.stringify(data)}`, 'info');
isSubscribed = false;
document.getElementById('subscribeBtn').disabled = false;
document.getElementById('unsubscribeBtn').disabled = true;
});
socket.on('disconnect', () => {
log('❌ WebSocket 连接已断开', 'error');
updateStatus(false);
});
socket.on('connect_error', (error) => {
log(`❌ 连接错误: ${error.message}`, 'error');
});
socket.on('error', (error) => {
log(`❌ 错误: ${error}`, 'error');
});
}
function subscribe() {
if (!socket || !socket.connected) {
log('❌ 请先连接 WebSocket', 'error');
return;
}
log('发送订阅请求...');
socket.emit('subscribe_events', {
event_type: 'all',
importance: 'all'
});
}
function unsubscribe() {
if (!socket || !socket.connected) {
log('❌ 请先连接 WebSocket', 'error');
return;
}
log('发送取消订阅请求...');
socket.emit('unsubscribe_events', {
event_type: 'all'
});
}
function disconnect() {
if (socket) {
socket.disconnect();
log('已手动断开连接');
updateStatus(false);
}
}
function clearLog() {
document.getElementById('log').innerHTML = '';
document.getElementById('events').innerHTML = '';
log('日志已清空');
}
// 页面加载时显示提示
window.onload = function() {
log('📌 测试步骤:');
log('1. 确保 Flask 服务已启动 (python app.py)');
log('2. 点击 "连接 WebSocket" 按钮');
log('3. 点击 "订阅所有事件" 按钮');
log('4. 等待后台脚本创建新事件,或等待轮询检测到新事件');
log('5. 新事件会自动推送到这个页面');
};
</script>
</body>
</html>