-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket-test.html
More file actions
73 lines (63 loc) · 2.4 KB
/
socket-test.html
File metadata and controls
73 lines (63 loc) · 2.4 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
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO Test</title>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
</head>
<body>
<h1>Socket.IO Connection Test</h1>
<div id="status">Connecting...</div>
<div id="logs"></div>
<script>
const logs = document.getElementById('logs');
const status = document.getElementById('status');
function addLog(message) {
const div = document.createElement('div');
div.textContent = new Date().toLocaleTimeString() + ': ' + message;
logs.appendChild(div);
console.log(message);
}
// Connect to your backend (running on port 8000)
const socket = io('http://localhost:8000', {
withCredentials: true,
transports: ['websocket', 'polling']
});
socket.on('connect', () => {
status.textContent = 'Connected ✅';
status.style.color = 'green';
addLog('Connected to server with ID: ' + socket.id);
// Emit a test join event
socket.emit('join', 'test-user-id');
addLog('Sent join event for test-user-id');
});
socket.on('disconnect', () => {
status.textContent = 'Disconnected ❌';
status.style.color = 'red';
addLog('Disconnected from server');
});
socket.on('connect_error', (error) => {
status.textContent = 'Connection Error ❌';
status.style.color = 'red';
addLog('Connection error: ' + error.message);
});
// Test message sending
setTimeout(() => {
if (socket.connected) {
socket.emit('join_chat', 'test-chat-id');
addLog('Sent join_chat event for test-chat-id');
setTimeout(() => {
socket.emit('send_message', {
chatId: 'test-chat-id',
content: 'Test message from browser',
sender: { name: 'Test User' }
});
addLog('Sent test message');
}, 1000);
}
}, 2000);
socket.on('receive_message', (message) => {
addLog('Received message: ' + JSON.stringify(message));
});
</script>
</body>
</html>