websocket_test.html
8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!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;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.container {
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.status {
padding: 10px;
border-radius: 4px;
margin-bottom: 10px;
}
.connected {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.disconnected {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.message-form {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.message-form input {
flex: 1;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.message-form button {
padding: 8px 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.message-form button:hover {
background-color: #0056b3;
}
.log {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 4px;
padding: 10px;
height: 300px;
overflow-y: auto;
font-family: monospace;
font-size: 12px;
}
.log-entry {
margin-bottom: 5px;
padding: 2px 0;
}
.log-entry.info {
color: #0066cc;
}
.log-entry.error {
color: #cc0000;
}
.log-entry.success {
color: #009900;
}
</style>
</head>
<body>
<h1>WebSocket通信测试</h1>
<div class="container">
<h3>连接状态</h3>
<div id="status" class="status disconnected">未连接</div>
<button id="connectBtn" onclick="connect()">连接</button>
<button id="disconnectBtn" onclick="disconnect()" disabled>断开连接</button>
</div>
<div class="container">
<h3>发送消息测试</h3>
<div class="message-form">
<input type="number" id="sessionid" placeholder="会话ID" value="0">
<select id="messageType">
<option value="chat">智能对话</option>
<option value="echo">回音模式</option>
</select>
<input type="text" id="messageText" placeholder="输入消息内容">
<button onclick="sendMessage()">发送到/human接口</button>
</div>
</div>
<div class="container">
<h3>消息日志</h3>
<button onclick="clearLog()">清空日志</button>
<div id="log" class="log"></div>
</div>
<script>
let ws = null;
let reconnectAttempts = 0;
const maxReconnectAttempts = 5;
function addLog(message, type = 'info') {
const log = document.getElementById('log');
const entry = document.createElement('div');
entry.className = `log-entry ${type}`;
const timestamp = new Date().toLocaleTimeString();
entry.textContent = `[${timestamp}] ${message}`;
log.appendChild(entry);
log.scrollTop = log.scrollHeight;
}
function updateStatus(connected) {
const status = document.getElementById('status');
const connectBtn = document.getElementById('connectBtn');
const disconnectBtn = document.getElementById('disconnectBtn');
if (connected) {
status.textContent = '已连接';
status.className = 'status connected';
connectBtn.disabled = true;
disconnectBtn.disabled = false;
} else {
status.textContent = '未连接';
status.className = 'status disconnected';
connectBtn.disabled = false;
disconnectBtn.disabled = true;
}
}
function connect() {
const host = window.location.hostname;
const port = '8010';
const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
const wsUrl = `${protocol}${host}:${port}/ws`;
addLog(`尝试连接到: ${wsUrl}`);
ws = new WebSocket(wsUrl);
ws.onopen = function() {
addLog('WebSocket连接成功', 'success');
updateStatus(true);
reconnectAttempts = 0;
// 发送登录消息
const sessionid = parseInt(document.getElementById('sessionid').value) || 0;
const loginMessage = {
type: 'login',
sessionid: sessionid,
username: 'TestUser'
};
ws.send(JSON.stringify(loginMessage));
addLog(`发送登录消息: ${JSON.stringify(loginMessage)}`);
};
ws.onmessage = function(e) {
addLog(`收到消息: ${e.data}`, 'success');
try {
const messageData = JSON.parse(e.data);
if (messageData.type === 'chat_message') {
const data = messageData.data;
addLog(`聊天消息 - 来源: ${data.source}, 类型: ${data.message_type}, 内容: ${data.content}`, 'success');
} else if (messageData.type === 'login_success') {
addLog(`登录成功: ${messageData.message}`, 'success');
} else if (messageData.type === 'pong') {
addLog('收到心跳响应', 'info');
}
} catch (err) {
addLog(`解析消息失败: ${err.message}`, 'error');
}
};
ws.onclose = function(e) {
addLog(`WebSocket连接关闭: ${e.code} - ${e.reason}`, 'error');
updateStatus(false);
// 自动重连
if (reconnectAttempts < maxReconnectAttempts) {
reconnectAttempts++;
addLog(`尝试重连 (${reconnectAttempts}/${maxReconnectAttempts})...`);
setTimeout(connect, 3000);
}
};
ws.onerror = function(e) {
addLog('WebSocket连接错误', 'error');
};
}
function disconnect() {
if (ws) {
ws.close();
ws = null;
}
}
async function sendMessage() {
const sessionid = parseInt(document.getElementById('sessionid').value) || 0;
const messageType = document.getElementById('messageType').value;
const messageText = document.getElementById('messageText').value;
if (!messageText.trim()) {
addLog('请输入消息内容', 'error');
return;
}
const payload = {
sessionid: sessionid,
type: messageType,
text: messageText
};
addLog(`发送到/human接口: ${JSON.stringify(payload)}`);
try {
const response = await fetch('/human', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
const result = await response.json();
addLog(`/human接口响应: ${JSON.stringify(result)}`, response.ok ? 'success' : 'error');
// 清空输入框
document.getElementById('messageText').value = '';
} catch (err) {
addLog(`发送失败: ${err.message}`, 'error');
}
}
function clearLog() {
document.getElementById('log').innerHTML = '';
}
// 页面加载时自动连接
window.onload = function() {
addLog('页面加载完成,准备测试WebSocket通信');
};
// 监听回车键发送消息
document.getElementById('messageText').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>