-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-client.html
More file actions
308 lines (270 loc) · 10.8 KB
/
test-client.html
File metadata and controls
308 lines (270 loc) · 10.8 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real-time Chat Test Client</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
border-radius: 10px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.status {
padding: 10px;
border-radius: 5px;
margin-bottom: 20px;
font-weight: bold;
}
.connected { background-color: #d4edda; color: #155724; }
.disconnected { background-color: #f8d7da; color: #721c24; }
.chat-messages {
height: 300px;
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 20px;
overflow-y: auto;
background-color: #fafafa;
border-radius: 5px;
}
.message {
margin-bottom: 10px;
padding: 8px;
border-radius: 5px;
}
.message.sent { background-color: #007bff; color: white; text-align: right; }
.message.received { background-color: #e9ecef; }
.message.system { background-color: #fff3cd; color: #856404; font-style: italic; }
.input-group {
display: flex;
gap: 10px;
margin-bottom: 10px;
}
input, button, select {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
button:hover { background-color: #0056b3; }
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.form-row {
display: flex;
gap: 10px;
align-items: center;
margin-bottom: 10px;
}
label {
min-width: 80px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 Real-time Chat Test Client</h1>
<div id="status" class="status disconnected">
❌ Disconnected
</div>
<div class="form-row">
<label>Connection:</label>
<input type="text" value="Socket.IO - Real-time chat working! ✅" style="flex: 1;" readonly>
<button id="connectBtn" onclick="connect()">Connect</button>
<button id="disconnectBtn" onclick="disconnect()" disabled>Disconnect</button>
</div>
<div class="form-row">
<label>Username:</label>
<input type="text" id="username" value="TestUser" style="flex: 1;">
</div>
<div class="form-row">
<label>Room:</label>
<select id="roomSelect">
<option value="general">General</option>
<option value="random">Random</option>
</select>
<button id="joinBtn" onclick="joinRoom()" disabled>Join Room</button>
<button id="leaveBtn" onclick="leaveRoom()" disabled>Leave Room</button>
</div>
<div class="chat-messages" id="messages"></div>
<div class="input-group">
<input type="text" id="messageInput" placeholder="Type your message..." style="flex: 1;" disabled>
<button id="sendBtn" onclick="sendMessage()" disabled>Send</button>
</div>
<div style="margin-top: 20px;">
<h3>📋 Instructions:</h3>
<ol>
<li>Click <strong>Connect</strong> to establish WebSocket connection</li>
<li>Enter your username and click <strong>Join Room</strong></li>
<li>Type messages and click <strong>Send</strong> to test real-time chat</li>
<li>Open multiple browser tabs to test multi-user chat</li>
</ol>
</div>
</div>
<script>
let socket = null;
let currentRoom = null;
let username = 'TestUser';
function log(message, type = 'system') {
const messagesDiv = document.getElementById('messages');
const messageEl = document.createElement('div');
messageEl.className = `message ${type}`;
messageEl.innerHTML = `<small>${new Date().toLocaleTimeString()}</small><br>${message}`;
messagesDiv.appendChild(messageEl);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
function updateStatus(connected) {
const statusEl = document.getElementById('status');
const connectBtn = document.getElementById('connectBtn');
const disconnectBtn = document.getElementById('disconnectBtn');
const joinBtn = document.getElementById('joinBtn');
const leaveBtn = document.getElementById('leaveBtn');
const messageInput = document.getElementById('messageInput');
const sendBtn = document.getElementById('sendBtn');
if (connected) {
statusEl.textContent = '✅ Connected';
statusEl.className = 'status connected';
connectBtn.disabled = true;
disconnectBtn.disabled = false;
joinBtn.disabled = false;
} else {
statusEl.textContent = '❌ Disconnected';
statusEl.className = 'status disconnected';
connectBtn.disabled = false;
disconnectBtn.disabled = true;
joinBtn.disabled = true;
leaveBtn.disabled = true;
messageInput.disabled = true;
sendBtn.disabled = true;
currentRoom = null;
}
}
function connect() {
username = document.getElementById('username').value || 'Anonymous';
// Socket.IO connection (the working approach!)
log('🚀 Connecting to real-time chat via Socket.IO...');
// Use local Socket.IO implementation
const script = document.createElement('script');
script.src = '/socket.io/socket.io.js';
script.onload = function() {
if (typeof io !== 'undefined') {
socket = io('http://localhost:3000');
socket.on('connect', function() {
log('✅ Connected to real-time chat server!');
updateStatus(true);
});
socket.on('disconnect', function() {
log('❌ Disconnected from chat server');
updateStatus(false);
});
socket.on('user-joined', function(data) {
log(`👋 ${data.message}`, 'system');
});
socket.on('new-message', function(data) {
log(`💬 ${data.sender}: ${data.content}`, 'received');
});
socket.on('user-left', function(data) {
log(`👋 ${data.message}`, 'system');
});
socket.on('connect_error', function(error) {
log(`💥 Connection error: ${error}`, 'system');
updateStatus(false);
});
} else {
log('❌ Socket.IO library not available');
}
};
script.onerror = function() {
log('❌ Failed to load Socket.IO library');
updateStatus(false);
};
document.head.appendChild(script);
}
function disconnect() {
if (socket) {
socket.close();
socket = null;
}
}
function joinRoom() {
if (!socket) return;
const roomId = document.getElementById('roomSelect').value;
currentRoom = roomId;
log(`📝 Joining room: ${roomId}`);
// Use Socket.IO to emit join event
if (socket && socket.emit) {
socket.emit('join', {
username: username,
roomId: roomId
});
} else {
log('❌ Socket not connected or emit not available');
}
document.getElementById('leaveBtn').disabled = false;
document.getElementById('messageInput').disabled = false;
document.getElementById('sendBtn').disabled = false;
}
function leaveRoom() {
if (!socket || !currentRoom) return;
log(`👋 Leaving room: ${currentRoom}`);
try {
socket.send(JSON.stringify({
event: 'leave',
data: {
username: username,
roomId: currentRoom
}
}));
} catch (e) {
log(`Error sending leave message: ${e}`);
}
currentRoom = null;
document.getElementById('leaveBtn').disabled = true;
document.getElementById('messageInput').disabled = true;
document.getElementById('sendBtn').disabled = true;
}
function sendMessage() {
if (!socket || !currentRoom) return;
const messageInput = document.getElementById('messageInput');
const messageText = messageInput.value.trim();
if (!messageText) return;
log(`💬 ${username}: ${messageText}`, 'sent');
// Use Socket.IO to emit message
if (socket && socket.emit) {
socket.emit('message', {
username: username,
message: messageText,
roomId: currentRoom
});
} else {
log('❌ Socket not connected or emit not available');
}
messageInput.value = '';
}
// Enter key to send message
document.getElementById('messageInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
sendMessage();
}
});
// Initial setup
updateStatus(false);
log('🚀 Chat client loaded. Click Connect to start!');
</script>
</body>
</html>