ws-go-server/static/index.html
2025-05-18 23:54:04 +02:00

159 lines
5.9 KiB
HTML

<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Debug Console</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- DaisyUI -->
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.7.3/dist/full.min.css" rel="stylesheet" type="text/css" />
<!-- Prism.js -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-json.min.js"></script>
<style>
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.status-pulse {
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}
#messages {
max-height: 500px;
overflow-y: auto;
}
#messages pre {
margin: 0;
padding: 0.5rem;
background: transparent !important;
}
.message-entry {
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
padding: 0.5rem 0;
}
.message-entry:last-child {
border-bottom: none;
}
.timestamp {
color: #888;
font-size: 0.875rem;
margin-right: 0.5rem;
}
</style>
</head>
<body class="min-h-screen bg-base-200">
<div class="container mx-auto px-4 py-8">
<div class="flex flex-col items-center space-y-6">
<!-- Header -->
<div class="text-center">
<h1 class="text-4xl font-bold text-primary mb-2">WebSocket Debug Console</h1>
<div class="flex items-center justify-center space-x-2">
<div id="status-indicator" class="w-3 h-3 rounded-full bg-error"></div>
<span id="status" class="text-lg font-semibold">Connecting...</span>
</div>
</div>
<!-- Main Content -->
<div class="w-full max-w-4xl">
<div class="card bg-base-100 shadow-xl">
<div class="card-body">
<h2 class="card-title text-xl mb-4">WebSocket Messages</h2>
<div id="messages" class="font-mono text-sm bg-base-300 rounded-lg p-4">
<!-- Messages will be inserted here -->
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://rawgit.com/kawanet/msgpack-lite/master/dist/msgpack.min.js"></script>
<script>
// Local WebSocket
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
const wsUrl = `${protocol}://${window.location.host}/api/v2/ws`;
let ws;
function updateStatus(connected, message) {
const statusIndicator = document.getElementById('status-indicator');
const statusText = document.getElementById('status');
if (connected) {
statusIndicator.classList.remove('bg-error');
statusIndicator.classList.add('bg-success');
statusIndicator.classList.remove('status-pulse');
} else {
statusIndicator.classList.remove('bg-success');
statusIndicator.classList.add('bg-error');
statusIndicator.classList.add('status-pulse');
}
statusText.textContent = message;
}
function addMessage(data) {
const messagesContainer = document.getElementById('messages');
const timestamp = new Date().toISOString();
const messageDiv = document.createElement('div');
messageDiv.className = 'message-entry';
const timestampSpan = document.createElement('span');
timestampSpan.className = 'timestamp';
timestampSpan.textContent = `[${timestamp}]`;
const code = document.createElement('code');
code.className = 'language-json';
code.textContent = JSON.stringify(data);
messageDiv.appendChild(timestampSpan);
messageDiv.appendChild(code);
messagesContainer.insertBefore(messageDiv, messagesContainer.firstChild);
Prism.highlightElement(code);
}
function connect() {
ws = new WebSocket(wsUrl);
ws.binaryType = 'arraybuffer';
ws.onopen = function() {
updateStatus(true, 'Connected');
console.log('WebSocket connection opened');
ws.send(msgpack.encode({
type: "subscribe",
eventType: "blockheight"
}));
ws.send(msgpack.encode({
type: "subscribe",
eventType: "mempool-fee-rate"
}));
ws.send(msgpack.encode({
type: "subscribe",
eventType: "price",
currencies: ["USD", "EUR"]
}));
};
ws.onmessage = function(event) {
const data = msgpack.decode(new Uint8Array(event.data));
addMessage(data);
};
ws.onclose = function() {
updateStatus(false, 'Disconnected. Reconnecting...');
console.log('WebSocket connection closed');
setTimeout(connect, 1000);
};
ws.onerror = function(error) {
updateStatus(false, 'Connection Error');
console.error('WebSocket error:', error);
};
}
connect();
</script>
</body>
</html>