-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket-test.html
More file actions
170 lines (146 loc) · 6.25 KB
/
socket-test.html
File metadata and controls
170 lines (146 loc) · 6.25 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Socket.io Connection Test</title>
<script src="https://cdn.socket.io/4.6.1/socket.io.min.js"></script>
<style>
.button {
margin: 10px 0;
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.button:hover {
background-color: #45a049;
}
#messages {
margin-top: 20px;
border: 1px solid #ddd;
padding: 10px;
height: 300px;
overflow-y: auto;
}
</style>
</head>
<body>
<h1>Socket.io Connection Test</h1>
<div id="status">Not connected</div>
<button id="checkHttp" class="button">1. Check HTTP Connection</button>
<button id="tryWebsocket" class="button">2. Try WebSocket</button>
<button id="tryPolling" class="button">3. Try Polling</button>
<button id="trySecure" class="button">4. Try Secure Connection (HTTPS)</button>
<div id="messages"></div>
<script>
const statusEl = document.getElementById('status');
const messagesEl = document.getElementById('messages');
let socket;
// Check HTTP connection first
document.getElementById('checkHttp').addEventListener('click', () => {
addMessage('Checking HTTP connection to server...');
statusEl.textContent = 'Checking HTTP...';
statusEl.style.color = 'blue';
fetch('http://io.click2call.ai:3002')
.then(response => {
addMessage(`HTTP Response: ${response.status} ${response.statusText}`);
if (response.ok) {
statusEl.textContent = 'HTTP Connection Successful';
statusEl.style.color = 'green';
return response.text();
} else {
statusEl.textContent = `HTTP Error: ${response.status}`;
statusEl.style.color = 'orange';
throw new Error(`HTTP error: ${response.status}`);
}
})
.then(data => {
addMessage(`Server response: ${data.substring(0, 100)}${data.length > 100 ? '...' : ''}`);
})
.catch(error => {
addMessage(`HTTP connection error: ${error.message}`);
statusEl.textContent = `HTTP Connection Failed: ${error.message}`;
statusEl.style.color = 'red';
});
});
// Try WebSocket connection
document.getElementById('tryWebsocket').addEventListener('click', () => {
if (socket) {
socket.disconnect();
}
addMessage('Trying WebSocket connection...');
statusEl.textContent = 'Connecting via WebSocket...';
statusEl.style.color = 'blue';
socket = io('http://io.click2call.ai:3002', {
transports: ['websocket'],
reconnectionAttempts: 3,
timeout: 10000
});
setupSocketListeners('WebSocket');
});
// Try Polling connection
document.getElementById('tryPolling').addEventListener('click', () => {
if (socket) {
socket.disconnect();
}
addMessage('Trying Polling connection...');
statusEl.textContent = 'Connecting via Polling...';
statusEl.style.color = 'blue';
socket = io('http://io.click2call.ai:3002', {
transports: ['polling'],
reconnectionAttempts: 3,
timeout: 10000
});
setupSocketListeners('Polling');
});
// Try Secure connection
document.getElementById('trySecure').addEventListener('click', () => {
if (socket) {
socket.disconnect();
}
addMessage('Trying Secure connection (HTTPS)...');
statusEl.textContent = 'Connecting via HTTPS...';
statusEl.style.color = 'blue';
socket = io('https://io.click2call.ai:3002', {
transports: ['websocket', 'polling'],
reconnectionAttempts: 3,
timeout: 10000
});
setupSocketListeners('Secure');
});
function setupSocketListeners(type) {
socket.on('connect', () => {
statusEl.textContent = `Connected (${type})! Socket ID: ${socket.id}`;
statusEl.style.color = 'green';
addMessage(`Connected to server using ${type}`);
});
socket.on('connect_error', (error) => {
statusEl.textContent = `Connection Error (${type}): ${error.message}`;
statusEl.style.color = 'red';
addMessage(`Connection error with ${type}: ${error.message}`);
console.error(`${type} connection error details:`, error);
});
socket.on('disconnect', (reason) => {
statusEl.textContent = `Disconnected (${type}): ${reason}`;
statusEl.style.color = 'orange';
addMessage(`Disconnected (${type}): ${reason}`);
});
socket.onAny((eventName, ...args) => {
addMessage(`Received event: ${eventName}, data: ${JSON.stringify(args)}`);
});
}
function addMessage(message) {
const messageEl = document.createElement('div');
messageEl.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
messagesEl.appendChild(messageEl);
console.log(message);
// Auto-scroll to bottom
messagesEl.scrollTop = messagesEl.scrollHeight;
}
addMessage('Test page loaded. Click buttons to test connection.');
</script>
</body>
</html>