-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
422 lines (384 loc) · 13.7 KB
/
test.html
File metadata and controls
422 lines (384 loc) · 13.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FastAPI Notify WS - Test Page</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-bottom: 30px;
}
.status {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 20px;
font-weight: 600;
font-size: 14px;
color: #333;
}
.status-dot {
width: 12px;
height: 12px;
border-radius: 50%;
background-color: #ccc;
transition: background-color 0.3s ease;
}
.status.connected .status-dot {
background-color: #4CAF50;
}
.status.disconnected .status-dot {
background-color: #f44336;
}
.status.connecting .status-dot {
background-color: #ff9800;
animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.controls {
display: flex;
gap: 10px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.token-input-group {
display: flex;
gap: 10px;
flex: 1;
min-width: 300px;
margin-bottom: 20px;
}
input[type="text"], textarea {
flex: 1;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 6px;
font-size: 14px;
font-family: 'Monaco', 'Menlo', monospace;
}
input[type="text"]:focus, textarea:focus {
outline: none;
border-color: #2196F3;
}
textarea {
width: 100%;
min-height: 80px;
resize: vertical;
}
button {
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
font-weight: 600;
transition: all 0.2s;
}
button:hover {
transform: translateY(-1px);
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
button.primary {
background-color: #2196F3;
color: white;
}
button.secondary {
background-color: #757575;
color: white;
}
button.success {
background-color: #4CAF50;
color: white;
}
button:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.messages {
border: 2px solid #e0e0e0;
border-radius: 8px;
padding: 16px;
height: 400px;
overflow-y: auto;
margin-bottom: 20px;
background-color: #fafafa;
}
.message {
padding: 12px;
margin: 8px 0;
border-radius: 6px;
background-color: white;
border-left: 4px solid #2196F3;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.message.error {
border-left-color: #f44336;
background-color: #ffebee;
}
.message.success {
border-left-color: #4CAF50;
background-color: #e8f5e9;
}
.message.warning {
border-left-color: #ff9800;
background-color: #fff3e0;
}
.message-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.message-type {
font-weight: 600;
color: #333;
}
.message-time {
font-size: 12px;
color: #666;
}
.message-content {
font-family: 'Monaco', 'Menlo', monospace;
font-size: 13px;
white-space: pre-wrap;
word-break: break-word;
}
.test-section {
margin-top: 30px;
padding: 20px;
background-color: #f0f4f8;
border-radius: 8px;
}
.test-controls {
display: flex;
gap: 10px;
margin-top: 15px;
flex-wrap: wrap;
}
.info-box {
background-color: #e3f2fd;
border: 1px solid #2196F3;
border-radius: 6px;
padding: 15px;
margin-bottom: 20px;
}
.info-box h3 {
margin-top: 0;
color: #1976D2;
}
.code {
background-color: #f5f5f5;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Monaco', 'Menlo', monospace;
font-size: 13px;
}
</style>
</head>
<body>
<div class="container">
<h1>🔌 FastAPI Notify WebSocket Test</h1>
<div class="info-box">
<h3>Connection Info</h3>
<p>WebSocket URL: <span class="code">ws://localhost:8000/ws?token=YOUR_TOKEN</span></p>
<p>Make sure your FastAPI server is running with the WebSocket endpoint configured.</p>
</div>
<div id="status" class="status disconnected">
<div class="status-dot"></div>
<span>Disconnected</span>
</div>
<div class="token-input-group">
<input type="text" id="token" placeholder="Paste your JWT token here..." value="">
<button class="primary" onclick="connect()">Connect</button>
<button class="secondary" onclick="disconnect()">Disconnect</button>
</div>
<h3>📨 Messages</h3>
<div id="messages" class="messages"></div>
<div class="test-section">
<h3>🧪 Test Notifications</h3>
<div style="margin-bottom: 15px;">
<label>User ID (must match token):</label>
<input type="text" id="userId" placeholder="e.g., user-123 or actual user ID" style="width: 300px; margin-top: 5px;">
</div>
<div style="margin-bottom: 15px;">
<label>Test Message:</label>
<textarea id="testMessage" placeholder="Enter notification message...">{
"message": "Hello from test page!",
"timestamp": "2025-01-07T10:00:00Z"
}</textarea>
</div>
<div class="test-controls">
<button class="success" onclick="sendPing()">Send Ping</button>
<button class="primary" onclick="sendCustomMessage()">Send Custom Message</button>
</div>
</div>
</div>
<script>
let ws = null;
let reconnectTimeout = null;
let messageCount = 0;
function updateStatus(status, text) {
const statusEl = document.getElementById('status');
const statusText = statusEl.querySelector('span');
statusEl.className = `status ${status}`;
statusText.textContent = text || status.charAt(0).toUpperCase() + status.slice(1);
}
function addMessage(data, type = 'info') {
const messagesEl = document.getElementById('messages');
const messageEl = document.createElement('div');
messageEl.className = `message ${type}`;
const time = new Date().toLocaleTimeString();
const messageType = data.type || 'unknown';
messageEl.innerHTML = `
<div class="message-header">
<span class="message-type">${messageType}</span>
<span class="message-time">${time}</span>
</div>
<div class="message-content">${JSON.stringify(data, null, 2)}</div>
`;
messagesEl.appendChild(messageEl);
messagesEl.scrollTop = messagesEl.scrollHeight;
messageCount++;
// Limit messages to prevent memory issues
if (messageCount > 100) {
messagesEl.removeChild(messagesEl.firstChild);
messageCount--;
}
}
function connect() {
if (ws && ws.readyState === WebSocket.OPEN) {
addMessage({ message: 'Already connected' }, 'warning');
return;
}
const token = document.getElementById('token').value.trim();
if (!token) {
alert('Please enter a token');
return;
}
updateStatus('connecting', 'Connecting...');
const wsUrl = `ws://localhost:8000/ws?token=${encodeURIComponent(token)}`;
console.log('Connecting to:', wsUrl);
try {
ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log('WebSocket connected');
updateStatus('connected');
addMessage({ type: 'system', message: 'Connected successfully' }, 'success');
// Clear any reconnect timeout
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
reconnectTimeout = null;
}
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
console.log('Received:', data);
// Determine message type for styling
let messageType = 'info';
if (data.type === 'error') messageType = 'error';
else if (data.type === 'success' || data.type === 'connection') messageType = 'success';
else if (data.type === 'warning') messageType = 'warning';
addMessage(data, messageType);
} catch (error) {
console.error('Failed to parse message:', error);
addMessage({
type: 'parse_error',
message: event.data,
error: error.message
}, 'error');
}
};
ws.onclose = (event) => {
console.log('WebSocket closed:', event.code, event.reason);
updateStatus('disconnected');
if (event.code === 1008) {
addMessage({
type: 'auth_error',
message: 'Authentication failed. Check your token.',
code: event.code,
reason: event.reason
}, 'error');
} else {
addMessage({
type: 'disconnected',
message: `Connection closed: ${event.reason || 'Unknown reason'}`,
code: event.code
}, 'warning');
}
ws = null;
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
addMessage({
type: 'connection_error',
message: 'Failed to connect. Is the server running?'
}, 'error');
};
} catch (error) {
console.error('Failed to create WebSocket:', error);
updateStatus('disconnected');
addMessage({
type: 'error',
message: `Failed to connect: ${error.message}`
}, 'error');
}
}
function disconnect() {
if (ws) {
ws.close(1000, 'User disconnect');
ws = null;
}
updateStatus('disconnected');
}
function sendPing() {
if (!ws || ws.readyState !== WebSocket.OPEN) {
alert('Not connected');
return;
}
const message = {
type: 'ping',
timestamp: Date.now()
};
ws.send(JSON.stringify(message));
addMessage({ type: 'sent', payload: message }, 'info');
}
function sendCustomMessage() {
if (!ws || ws.readyState !== WebSocket.OPEN) {
alert('Not connected');
return;
}
const messageText = document.getElementById('testMessage').value;
try {
const message = JSON.parse(messageText);
ws.send(JSON.stringify(message));
addMessage({ type: 'sent', payload: message }, 'info');
} catch (error) {
alert('Invalid JSON: ' + error.message);
}
}
// Example token for testing
document.getElementById('token').placeholder = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
// Auto-focus token input
document.getElementById('token').focus();
</script>
</body>
</html>