-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
243 lines (204 loc) · 9 KB
/
Copy pathtest.html
File metadata and controls
243 lines (204 loc) · 9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tornado Websockets (Redis Streams)</title>
<style>
body { font-family: monospace; padding: 10px; max-width: 800px; margin: 0 auto; }
.section { border: 1px solid #ccc; padding: 10px; margin-bottom: 10px; }
h3 { margin-top: 0; }
input { padding: 5px; margin-right: 5px; }
button { padding: 5px 10px; cursor: pointer; }
button:disabled { background-color: #ddd; color: #888; cursor: not-allowed; }
#log {
height: 400px;
border: 1px solid #000;
overflow-y: scroll;
padding: 5px;
background: #f0f0f0;
font-size: 12px;
}
.log-item { border-bottom: 1px solid #ddd; padding: 2px 0; }
.ts { color: #888; margin-right: 5px; }
.direction-in { color: green; font-weight: bold; }
.direction-out { color: blue; font-weight: bold; }
.sys { color: purple; font-weight: bold; }
.error { color: red; }
</style>
</head>
<body>
<div class="section">
<h3>1. Connection</h3>
<input type="text" id="wsUrl" value="" style="width: 250px;">
<button id="btnConnect" onclick="toggleConnection()">Connect</button>
<button onclick="randomizePort()" style="font-size: 10px;">Port</button>
<span id="status" style="font-weight: bold; color: red;">OFFLINE</span>
<br><br>
<div>Client ID: <span id="clientId">-</span></div>
</div>
<div class="section">
<h3>2. Rooms</h3>
<input type="text" id="roomName" value="room1">
<button onclick="sendJson('join', {room: getVal('roomName')})">Join</button>
<button onclick="sendJson('leave', {room: getVal('roomName')})">Leave</button>
<button onclick="sendJson('get_rooms', {})">Get My Rooms</button>
</div>
<div class="section">
<h3>3. Messaging</h3>
<input type="text" id="msgInput" placeholder="Message text" style="width: 60%">
<button onclick="sendTextMessage()">Send</button>
<button onclick="sendBroadcast()">Broadcast All</button>
</div>
<div class="section">
<h3>4. Benchmark</h3>
Count: <input type="number" id="bmCount" value="100" style="width: 50px">
Delay(ms): <input type="number" id="bmDelay" value="10" style="width: 50px">
<button id="btnRunBm" onclick="runBenchmark()">Run Test</button>
<button id="btnStopBm" onclick="stopBenchmark()" disabled>Stop</button>
</div>
<div class="section">
<h3>Logs <button onclick="document.getElementById('log').innerHTML=''" style="font-size: 10px">Clear</button></h3>
<div id="log"></div>
</div>
<script>
let ws = null;
let isConnected = false;
let benchmarkInterval = null;
const getVal = (id) => document.getElementById(id).value;
function log(msg, type = 'info') {
const box = document.getElementById('log');
const date = new Date().toLocaleTimeString();
const el = document.createElement('div');
el.className = 'log-item';
let label = '';
if (type === 'in') label = '<span class="direction-in">[RX]</span>';
else if (type === 'out') label = '<span class="direction-out">[TX]</span>';
else if (type === 'error') label = '<span class="error">[ERR]</span>';
else if (type === 'sys') label = '<span class="sys">[SYS]</span>';
el.innerHTML = `<span class="ts">${date}</span> ${label} ${msg}`;
box.appendChild(el);
box.scrollTop = box.scrollHeight;
}
function updateStatus(active) {
isConnected = active;
const el = document.getElementById('status');
const btn = document.getElementById('btnConnect');
if (active) {
el.innerText = "ONLINE";
el.style.color = "green";
btn.innerText = "Disconnect";
} else {
el.innerText = "OFFLINE";
el.style.color = "red";
btn.innerText = "Connect";
document.getElementById('clientId').innerText = "-";
}
}
function toggleConnection() {
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
ws.close();
return;
}
const url = getVal('wsUrl');
ws = new WebSocket(url);
ws.onopen = () => {
log(`Connected to ${url}`, 'sys');
updateStatus(true);
const defaultRoom = getVal('roomName');
if(defaultRoom) {
log(`Auto-joining ${defaultRoom}...`, 'sys');
sendJson('join', { room: defaultRoom });
}
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
log(JSON.stringify(data), 'in');
if (data.event === 'connected') {
document.getElementById('clientId').innerText = data.data.client_id;
}
} catch (e) {
log(`Raw: ${event.data}`, 'in');
}
};
ws.onclose = () => {
log("Disconnected", 'sys');
updateStatus(false);
stopBenchmark();
};
ws.onerror = (err) => {
log("Socket Error. Ensure server is running on this port.", 'error');
};
}
function sendJson(eventName, dataPayload) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
log("Not connected!", 'error');
return;
}
const payload = JSON.stringify({
event: eventName,
data: dataPayload
});
ws.send(payload);
log(payload, 'out');
}
function sendTextMessage() {
sendJson('send_message', { text: getVal('msgInput') });
document.getElementById('msgInput').value = '';
}
function sendBroadcast() {
sendJson('broadcast', { text: getVal('msgInput') });
document.getElementById('msgInput').value = '';
}
function runBenchmark() {
if (!isConnected) return alert("Connect first!");
if (benchmarkInterval) return;
const count = parseInt(getVal('bmCount'));
const delay = parseInt(getVal('bmDelay'));
let sent = 0;
document.getElementById('btnRunBm').disabled = true;
document.getElementById('btnStopBm').disabled = false;
log(`Benchmark Started: ${count} msgs, ${delay}ms delay`, 'sys');
benchmarkInterval = setInterval(() => {
if (sent >= count) {
stopBenchmark(true);
return;
}
ws.send(JSON.stringify({
event: 'send_message',
data: { text: `BM #${sent + 1}` }
}));
sent++;
}, delay);
}
function stopBenchmark(completed = false) {
if (benchmarkInterval) {
clearInterval(benchmarkInterval);
benchmarkInterval = null;
document.getElementById('btnRunBm').disabled = false;
document.getElementById('btnStopBm').disabled = true;
log(completed ? "Benchmark Completed." : "Benchmark Stopped manually.", 'sys');
}
}
/* randomize was written for testing purposes with multiple server instances
more clearly to test horizontal scaling
for example if you run 4 server with ports 5000, 5001, 5002, 5003
each time the page is reloaded it will connect to a random port among these
const randomPort = Math.floor(Math.random() * 4) + 5000;
*/
function randomizePort() {
const randomPort = Math.floor(Math.random() * 1) + 5000;
const newUrl = `ws://localhost:${randomPort}/ws`;
document.getElementById('wsUrl').value = newUrl;
return randomPort;
}
window.addEventListener('load', () => {
const port = randomizePort();
log(`Initialized with Random Port: ${port}`, 'sys');
log("Auto-connecting...", 'sys');
toggleConnection();
});
</script>
</body>
</html>