-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-websocket.html
More file actions
71 lines (60 loc) · 2.39 KB
/
test-websocket.html
File metadata and controls
71 lines (60 loc) · 2.39 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
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO Test</title>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<h1>Socket.IO Connection Test</h1>
<div id="status">Connecting...</div>
<div id="messages"></div>
<button onclick="sendTestMessage()">Send Test Message</button>
<button onclick="sendPing()">Send Ping</button>
<script>
const statusDiv = document.getElementById('status');
const messagesDiv = document.getElementById('messages');
// Connect to Socket.IO server
const socket = io('http://localhost:4001');
socket.on('connect', function() {
statusDiv.innerHTML = 'Connected to Socket.IO server';
statusDiv.style.color = 'green';
console.log('Connected to Socket.IO server with ID:', socket.id);
});
socket.on('message', function(message) {
const messageElement = document.createElement('div');
messageElement.innerHTML = `<strong>Received:</strong> ${JSON.stringify(message, null, 2)}`;
messagesDiv.appendChild(messageElement);
});
socket.on('disconnect', function() {
statusDiv.innerHTML = 'Connection closed';
statusDiv.style.color = 'red';
});
socket.on('error', function(error) {
statusDiv.innerHTML = 'Connection error: ' + error;
statusDiv.style.color = 'red';
});
function sendTestMessage() {
const testMessage = {
type: 'api_request',
requestId: 'test-' + Date.now(),
payload: {
api: 'test.api.call',
data: { key: 'value' }
}
};
socket.emit('message', testMessage);
const messageElement = document.createElement('div');
messageElement.innerHTML = `<strong>Sent:</strong> ${JSON.stringify(testMessage, null, 2)}`;
messageElement.style.color = 'blue';
messagesDiv.appendChild(messageElement);
}
function sendPing() {
socket.emit('message', { type: 'ping' });
const messageElement = document.createElement('div');
messageElement.innerHTML = '<strong>Sent:</strong> ping';
messageElement.style.color = 'blue';
messagesDiv.appendChild(messageElement);
}
</script>
</body>
</html>