File size: 12,144 Bytes
b068b76 |
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 |
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>تست اتصال WebSocket</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="/static/css/connection-status.css">
<style>
body {
padding-top: 50px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.main-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.stats-card {
background: white;
border-radius: 15px;
padding: 30px;
box-shadow: 0 10px 40px rgba(0,0,0,0.15);
margin-bottom: 20px;
}
.big-stat {
text-align: center;
padding: 30px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 15px;
color: white;
margin-bottom: 20px;
}
.big-stat-value {
font-size: 72px;
font-weight: bold;
margin: 0;
animation: scaleIn 0.5s ease;
}
.big-stat-label {
font-size: 20px;
opacity: 0.9;
margin-top: 10px;
}
@keyframes scaleIn {
from { transform: scale(0.5); opacity: 0; }
to { transform: scale(1); opacity: 1; }
}
.message-log {
background: #f8f9fa;
border-radius: 10px;
padding: 20px;
max-height: 400px;
overflow-y: auto;
font-family: 'Courier New', monospace;
font-size: 13px;
}
.message-item {
padding: 8px;
margin-bottom: 5px;
border-radius: 5px;
animation: fadeIn 0.3s ease;
}
.message-sent {
background: #e3f2fd;
border-left: 3px solid #2196f3;
}
.message-received {
background: #e8f5e9;
border-left: 3px solid #4caf50;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
.control-panel {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
margin-top: 20px;
}
.btn-action {
padding: 12px 20px;
font-weight: 600;
border-radius: 8px;
transition: all 0.3s ease;
}
.btn-action:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
</style>
</head>
<body>
<!-- Connection Status Bar -->
<div class="connection-status-bar" id="ws-connection-status">
<div class="ws-connection-info">
<span class="status-dot status-dot-offline" id="ws-status-dot"></span>
<span class="ws-status-text" id="ws-status-text">در حال اتصال...</span>
<span class="connection-spinner"></span>
</div>
<div class="online-users-widget">
<div class="online-users-count">
<span class="users-icon">👥</span>
<span class="count-number" id="active-users-count">0</span>
<span class="count-label">کاربر آنلاین</span>
</div>
<div class="online-users-count">
<span class="users-icon">📊</span>
<span class="count-number" id="total-sessions-count">0</span>
<span class="count-label">جلسات کل</span>
</div>
</div>
</div>
<!-- Alerts Container -->
<div class="alerts-container" id="alerts-container"></div>
<!-- Main Content -->
<div class="main-container">
<div class="big-stat">
<div class="big-stat-value" id="active-users-big">0</div>
<div class="big-stat-label">کاربر در حال حاضر آنلاین هستند</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="stats-card">
<h3>📊 آمار اتصالات</h3>
<hr>
<table class="table">
<tr>
<td>اتصالات فعال:</td>
<td><strong id="stat-active">0</strong></td>
</tr>
<tr>
<td>جلسات کل:</td>
<td><strong id="stat-total">0</strong></td>
</tr>
<tr>
<td>پیامهای ارسالی:</td>
<td><strong id="stat-sent">0</strong></td>
</tr>
<tr>
<td>پیامهای دریافتی:</td>
<td><strong id="stat-received">0</strong></td>
</tr>
<tr>
<td>Session ID:</td>
<td><code id="session-id">-</code></td>
</tr>
</table>
<h5>انواع کلاینتها:</h5>
<div id="client-types-list"></div>
</div>
</div>
<div class="col-md-6">
<div class="stats-card">
<h3>🎮 کنترلها</h3>
<hr>
<div class="control-panel">
<button class="btn btn-primary btn-action" onclick="requestStats()">
📊 درخواست آمار
</button>
<button class="btn btn-success btn-action" onclick="subscribeToMarket()">
✅ Subscribe به Market
</button>
<button class="btn btn-warning btn-action" onclick="unsubscribeFromMarket()">
❌ Unsubscribe از Market
</button>
<button class="btn btn-info btn-action" onclick="sendPing()">
🏓 ارسال Ping
</button>
<button class="btn btn-danger btn-action" onclick="disconnect()">
🔌 قطع اتصال
</button>
<button class="btn btn-secondary btn-action" onclick="reconnect()">
🔄 اتصال مجدد
</button>
</div>
</div>
<div class="stats-card">
<h3>📝 لاگ پیامها</h3>
<hr>
<div class="message-log" id="message-log"></div>
<button class="btn btn-sm btn-outline-secondary mt-2" onclick="clearLog()">
🗑️ پاک کردن لاگ
</button>
</div>
</div>
</div>
</div>
<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="/static/js/websocket-client.js"></script>
<script>
// تنظیم handler برای Session ID
if (window.wsClient) {
window.wsClient.on('welcome', (message) => {
document.getElementById('session-id').textContent = message.session_id;
logMessage('دریافت', message);
});
window.wsClient.on('stats_update', (message) => {
updateStatsDisplay(message.data);
logMessage('دریافت', message);
});
window.wsClient.on('subscribed', (message) => {
logMessage('دریافت', message);
});
window.wsClient.on('pong', (message) => {
logMessage('دریافت', message);
});
}
// توابع کنترلی
function requestStats() {
if (window.wsClient) {
window.wsClient.requestStats();
logMessage('ارسال', { type: 'get_stats' });
}
}
function subscribeToMarket() {
if (window.wsClient) {
window.wsClient.subscribe('market');
logMessage('ارسال', { type: 'subscribe', group: 'market' });
}
}
function unsubscribeFromMarket() {
if (window.wsClient) {
window.wsClient.unsubscribe('market');
logMessage('ارسال', { type: 'unsubscribe', group: 'market' });
}
}
function sendPing() {
if (window.wsClient) {
window.wsClient.send({ type: 'ping' });
logMessage('ارسال', { type: 'ping' });
}
}
function disconnect() {
if (window.wsClient) {
window.wsClient.close();
logMessage('سیستم', 'قطع اتصال توسط کاربر');
}
}
function reconnect() {
if (window.wsClient) {
window.wsClient.reconnectAttempts = 0;
window.wsClient.connect();
logMessage('سیستم', 'تلاش برای اتصال مجدد');
}
}
function updateStatsDisplay(data) {
document.getElementById('stat-active').textContent = data.active_connections || 0;
document.getElementById('stat-total').textContent = data.total_sessions || 0;
document.getElementById('stat-sent').textContent = data.messages_sent || 0;
document.getElementById('stat-received').textContent = data.messages_received || 0;
// آپدیت عدد بزرگ
const bigValue = document.getElementById('active-users-big');
bigValue.textContent = data.active_connections || 0;
bigValue.style.animation = 'none';
setTimeout(() => bigValue.style.animation = 'scaleIn 0.5s ease', 10);
}
function logMessage(direction, message) {
const log = document.getElementById('message-log');
const item = document.createElement('div');
item.className = `message-item ${direction === 'ارسال' ? 'message-sent' : 'message-received'}`;
const time = new Date().toLocaleTimeString('fa-IR');
const content = typeof message === 'string' ? message : JSON.stringify(message, null, 2);
item.innerHTML = `
<strong>[${time}] ${direction}:</strong><br>
<pre style="margin:5px 0 0 0">${content}</pre>
`;
log.appendChild(item);
log.scrollTop = log.scrollHeight;
}
function clearLog() {
document.getElementById('message-log').innerHTML = '';
}
// آپدیت خودکار هر ۵ ثانیه
setInterval(() => {
if (window.wsClient && window.wsClient.isConnected) {
requestStats();
}
}, 5000);
</script>
</body>
</html>
|