Files
AUTO-MAS-test/websocket_test.html
MoeSnowyFox 1d91204842 feat(electron): 添加系统级对话框功能
- 在 ElectronAPI 中新增对话框相关方法:showQuestionDialog、dialogResponse、resizeDialogWindow
- 实现独立的对话框窗口 dialog.html,支持自定义标题、消息和选项- 添加对话框窗口的键盘导航和焦点管理功能
- 在主进程中实现对话框窗口的创建、显示和响应处理
- 更新 WebSocket 消息监听器组件,使用系统级对话框替代应用内弹窗- 优化进程清理逻辑,增加多种清理方法并行执行
- 重构日志工具类,改进 Electron API 调用方式
- 调整 Git 更新检查逻辑,避免直接访问 GitHub
- 移除冗余的类型定义文件,统一 Electron API 接口定义
2025-10-01 23:24:39 +08:00

225 lines
6.7 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);
}
.test-section {
margin-bottom: 20px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
margin: 5px;
}
button:hover {
background: #0056b3;
}
.status {
margin-top: 10px;
padding: 10px;
border-radius: 5px;
}
.status.connected {
background: #d4edda;
color: #155724;
}
.status.disconnected {
background: #f8d7da;
color: #721c24;
}
.log {
background: #f8f9fa;
border: 1px solid #dee2e6;
padding: 10px;
margin-top: 10px;
max-height: 300px;
overflow-y: auto;
font-family: monospace;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<h1>WebSocket消息测试工具</h1>
<div class="test-section">
<h3>连接状态</h3>
<button onclick="connectWebSocket()">连接WebSocket</button>
<button onclick="disconnectWebSocket()">断开连接</button>
<div id="status" class="status disconnected">未连接</div>
</div>
<div class="test-section">
<h3>测试消息</h3>
<button onclick="sendQuestionMessage()">发送Question消息</button>
<button onclick="sendObjectMessage()">发送普通对象消息</button>
<button onclick="sendStringMessage()">发送字符串消息</button>
<button onclick="sendMalformedMessage()">发送格式错误消息</button>
</div>
<div class="test-section">
<h3>消息日志</h3>
<button onclick="clearLog()">清空日志</button>
<div id="log" class="log"></div>
</div>
</div>
<script>
let ws = null;
function log(message) {
const logDiv = document.getElementById('log');
const timestamp = new Date().toLocaleTimeString();
logDiv.innerHTML += `[${timestamp}] ${message}\n`;
logDiv.scrollTop = logDiv.scrollHeight;
}
function updateStatus(connected) {
const statusDiv = document.getElementById('status');
if (connected) {
statusDiv.textContent = 'WebSocket已连接';
statusDiv.className = 'status connected';
} else {
statusDiv.textContent = 'WebSocket未连接';
statusDiv.className = 'status disconnected';
}
}
function connectWebSocket() {
if (ws && ws.readyState === WebSocket.OPEN) {
log('WebSocket已经连接');
return;
}
ws = new WebSocket('ws://localhost:36163/api/core/ws');
ws.onopen = function() {
log('WebSocket连接已建立');
updateStatus(true);
};
ws.onmessage = function(event) {
log('收到消息: ' + event.data);
try {
const data = JSON.parse(event.data);
log('解析后的消息: ' + JSON.stringify(data, null, 2));
} catch (e) {
log('消息解析失败: ' + e.message);
}
};
ws.onclose = function() {
log('WebSocket连接已关闭');
updateStatus(false);
};
ws.onerror = function(error) {
log('WebSocket错误: ' + error);
updateStatus(false);
};
}
function disconnectWebSocket() {
if (ws) {
ws.close();
ws = null;
log('主动断开WebSocket连接');
updateStatus(false);
}
}
function sendMessage(message) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
log('错误: WebSocket未连接');
return;
}
const messageStr = JSON.stringify(message);
ws.send(messageStr);
log('发送消息: ' + messageStr);
}
function sendQuestionMessage() {
const message = {
id: "test_id_" + Date.now(),
type: "message",
data: {
type: "Question",
message_id: "q_" + Date.now(),
title: "测试问题",
message: "这是一个测试问题,请选择是否继续?"
}
};
sendMessage(message);
}
function sendObjectMessage() {
const message = {
id: "test_obj_" + Date.now(),
type: "message",
data: {
action: "test_action",
status: "running",
content: "这是一个普通的对象消息"
}
};
sendMessage(message);
}
function sendStringMessage() {
const message = {
id: "test_str_" + Date.now(),
type: "message",
data: "这是一个字符串消息"
};
sendMessage(message);
}
function sendMalformedMessage() {
const message = {
id: "test_malformed_" + Date.now(),
type: "message",
data: {
type: "Question",
// 缺少 message_id
title: "格式错误的问题",
message: "这个消息缺少message_id字段"
}
};
sendMessage(message);
}
function clearLog() {
document.getElementById('log').innerHTML = '';
}
// 页面加载时自动连接
window.onload = function() {
log('页面已加载准备测试WebSocket连接');
};
</script>
</body>
</html>