Files
file-transfer-go/web/static/test-websocket.html
2025-07-28 18:12:05 +08:00

51 lines
1.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>
</head>
<body>
<h1>WebSocket连接测试</h1>
<div id="status">未连接</div>
<button onclick="testConnection()">测试连接</button>
<div id="log"></div>
<script>
function log(message) {
const logDiv = document.getElementById('log');
logDiv.innerHTML += '<div>' + new Date().toLocaleTimeString() + ': ' + message + '</div>';
console.log(message);
}
function testConnection() {
const code = '354888'; // 使用已存在的房间码
const role = 'receiver';
const wsUrl = `ws://localhost:8080/ws/p2p?code=${code}&role=${role}`;
log('尝试连接: ' + wsUrl);
const ws = new WebSocket(wsUrl);
ws.onopen = function() {
log('WebSocket连接成功!');
document.getElementById('status').textContent = '已连接';
};
ws.onerror = function(error) {
log('WebSocket错误: ' + JSON.stringify(error));
};
ws.onclose = function(event) {
log('WebSocket关闭: 代码=' + event.code + ', 原因=' + event.reason);
document.getElementById('status').textContent = '已断开';
};
ws.onmessage = function(event) {
log('收到消息: ' + event.data);
};
}
</script>
</body>
</html>