-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-demo.html
More file actions
355 lines (305 loc) · 12 KB
/
client-demo.html
File metadata and controls
355 lines (305 loc) · 12 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TON WebSocket Client Demo</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
.status {
padding: 10px;
border-radius: 4px;
margin-bottom: 10px;
font-weight: bold;
}
.status.connected { background-color: #d4edda; color: #155724; }
.status.disconnected { background-color: #f8d7da; color: #721c24; }
.status.connecting { background-color: #fff3cd; color: #856404; }
input, button {
padding: 10px;
margin: 5px;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
background-color: #007bff;
color: white;
cursor: pointer;
}
button:hover { background-color: #0056b3; }
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.log {
height: 400px;
overflow-y: scroll;
border: 1px solid #ddd;
padding: 10px;
background-color: #f8f9fa;
font-family: monospace;
font-size: 12px;
}
.log-entry {
margin-bottom: 5px;
word-wrap: break-word;
}
.log-entry.sent { color: #007bff; }
.log-entry.received { color: #28a745; }
.log-entry.error { color: #dc3545; }
.subscriptions {
margin-top: 20px;
}
.subscription-item {
padding: 10px;
border: 1px solid #ddd;
margin: 5px 0;
background-color: #f8f9fa;
border-radius: 4px;
}
.subscription-item button {
background-color: #dc3545;
font-size: 12px;
padding: 5px 10px;
}
.trace-display {
background-color: #f1f3f4;
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
margin: 10px 0;
}
.trace-header {
font-weight: bold;
color: #1a73e8;
margin-bottom: 10px;
}
.trace-details {
font-family: monospace;
font-size: 12px;
white-space: pre-wrap;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 TON WebSocket Service Demo</h1>
<div id="connection-status" class="status disconnected">
Отключено
</div>
<div>
<input type="text" id="server-url" placeholder="ws://localhost:3000/ws" value="ws://localhost:3000/ws">
<button id="connect-btn" onclick="toggleConnection()">Подключиться</button>
<button id="ping-btn" onclick="sendPing()" disabled>Ping</button>
</div>
</div>
<div class="container">
<h2>📡 Подписки на адреса</h2>
<div>
<input type="text" id="address-input" placeholder="Введите TON адрес"
value="EQCr6U28flePBIpdQYwmMkJJD2JAwZxeRE8C0RoxVJeEb-CF">
<button id="subscribe-btn" onclick="subscribeToAddress()" disabled>Подписаться</button>
</div>
<div class="subscriptions">
<h3>Активные подписки:</h3>
<div id="subscriptions-list"></div>
</div>
</div>
<div class="container">
<h2>📊 Последние трейсы транзакций</h2>
<div id="traces-container"></div>
</div>
<div class="container">
<h2>📝 Лог сообщений</h2>
<div id="log" class="log"></div>
<button onclick="clearLog()">Очистить лог</button>
</div>
<script>
let ws = null;
let subscriptions = new Map(); // subscription_id -> {id, address}
function updateConnectionStatus(status, message) {
const statusElement = document.getElementById('connection-status');
statusElement.className = `status ${status}`;
statusElement.textContent = message;
const connectBtn = document.getElementById('connect-btn');
const pingBtn = document.getElementById('ping-btn');
const subscribeBtn = document.getElementById('subscribe-btn');
if (status === 'connected') {
connectBtn.textContent = 'Отключиться';
pingBtn.disabled = false;
subscribeBtn.disabled = false;
} else {
connectBtn.textContent = 'Подключиться';
pingBtn.disabled = true;
subscribeBtn.disabled = true;
}
}
function log(message, type = 'info') {
const logElement = document.getElementById('log');
const entry = document.createElement('div');
entry.className = `log-entry ${type}`;
entry.innerHTML = `[${new Date().toLocaleTimeString()}] ${message}`;
logElement.appendChild(entry);
logElement.scrollTop = logElement.scrollHeight;
}
function clearLog() {
document.getElementById('log').innerHTML = '';
}
function toggleConnection() {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.close();
} else {
connect();
}
}
function connect() {
const url = document.getElementById('server-url').value;
updateConnectionStatus('connecting', 'Подключение...');
log('Подключение к ' + url, 'info');
ws = new WebSocket(url);
ws.onopen = function() {
updateConnectionStatus('connected', 'Подключено');
log('WebSocket соединение установлено', 'info');
};
ws.onmessage = function(event) {
try {
const message = JSON.parse(event.data);
handleServerMessage(message);
log('⬅️ ' + JSON.stringify(message, null, 2), 'received');
} catch (e) {
log('❌ Ошибка парсинга сообщения: ' + event.data, 'error');
}
};
ws.onerror = function(error) {
log('❌ WebSocket ошибка: ' + error, 'error');
};
ws.onclose = function() {
updateConnectionStatus('disconnected', 'Отключено');
log('WebSocket соединение закрыто', 'info');
subscriptions.clear();
updateSubscriptionsList();
};
}
function handleServerMessage(message) {
switch(message.type) {
case 'SubscriptionConfirm':
subscriptions.set(message.subscription_id, {
id: message.subscription_id,
address: message.address
});
updateSubscriptionsList();
log(`✅ Подписка создана: ${message.address}`, 'info');
break;
case 'UnsubscriptionConfirm':
subscriptions.delete(message.subscription_id);
updateSubscriptionsList();
log(`❌ Подписка удалена: ${message.subscription_id}`, 'info');
break;
case 'TransactionTrace':
displayTransactionTrace(message);
log(`🔄 Новый трейс транзакции для ${message.address}`, 'info');
break;
case 'Error':
log(`❌ Ошибка: ${message.message}`, 'error');
break;
case 'Pong':
log('🏓 Pong получен', 'info');
break;
default:
log(`❓ Неизвестный тип сообщения: ${message.type}`, 'error');
}
}
function sendMessage(message) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(message));
log('➡️ ' + JSON.stringify(message, null, 2), 'sent');
} else {
log('❌ WebSocket не подключен', 'error');
}
}
function sendPing() {
sendMessage({ type: 'Ping' });
}
function subscribeToAddress() {
const address = document.getElementById('address-input').value.trim();
if (!address) {
log('❌ Введите TON адрес', 'error');
return;
}
// Проверяем, не подписаны ли мы уже на этот адрес
for (let sub of subscriptions.values()) {
if (sub.address === address) {
log('⚠️ Уже подписаны на этот адрес', 'error');
return;
}
}
sendMessage({
type: 'Subscribe',
address: address
});
}
function unsubscribe(subscriptionId) {
sendMessage({
type: 'Unsubscribe',
subscription_id: subscriptionId
});
}
function updateSubscriptionsList() {
const container = document.getElementById('subscriptions-list');
if (subscriptions.size === 0) {
container.innerHTML = '<p>Нет активных подписок</p>';
return;
}
let html = '';
for (let [id, sub] of subscriptions) {
html += `
<div class="subscription-item">
<strong>Адрес:</strong> ${sub.address}<br>
<strong>ID:</strong> ${sub.id}<br>
<button onclick="unsubscribe('${sub.id}')">Отписаться</button>
</div>
`;
}
container.innerHTML = html;
}
function displayTransactionTrace(message) {
const container = document.getElementById('traces-container');
const trace = message.trace;
const traceElement = document.createElement('div');
traceElement.className = 'trace-display';
const header = document.createElement('div');
header.className = 'trace-header';
header.textContent = `Новый трейс для ${message.address}`;
const details = document.createElement('div');
details.className = 'trace-details';
details.textContent = JSON.stringify(trace, null, 2);
traceElement.appendChild(header);
traceElement.appendChild(details);
// Вставляем в начало контейнера
container.insertBefore(traceElement, container.firstChild);
// Ограничиваем количество отображаемых трейсов
while (container.children.length > 10) {
container.removeChild(container.lastChild);
}
}
// Автоподключение при загрузке страницы
window.addEventListener('load', function() {
// Можно раскомментировать для автоматического подключения
// connect();
});
</script>
</body>
</html>