← back to Watches

examples/websocket-client.html

317 lines

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Omega Watch WebSocket Client</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            border-bottom: 3px solid #d4af37;
            padding-bottom: 10px;
        }
        .status {
            padding: 15px;
            border-radius: 5px;
            margin: 20px 0;
            font-weight: bold;
        }
        .connected {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .disconnected {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .controls {
            display: flex;
            gap: 10px;
            margin: 20px 0;
        }
        button {
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.3s;
        }
        button.primary {
            background: #d4af37;
            color: white;
        }
        button.primary:hover {
            background: #c19f2f;
        }
        button.secondary {
            background: #6c757d;
            color: white;
        }
        button.secondary:hover {
            background: #5a6268;
        }
        select, input {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        .messages {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 20px;
            height: 400px;
            overflow-y: auto;
            font-family: 'Courier New', monospace;
            font-size: 13px;
        }
        .message {
            padding: 8px;
            margin: 5px 0;
            border-radius: 3px;
            background: white;
            border-left: 3px solid #d4af37;
        }
        .message.sent {
            border-left-color: #28a745;
        }
        .message.received {
            border-left-color: #007bff;
        }
        .message.error {
            border-left-color: #dc3545;
            background: #fff5f5;
        }
        .timestamp {
            color: #6c757d;
            font-size: 11px;
        }
        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin: 20px 0;
        }
        .stat-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
        }
        .stat-value {
            font-size: 32px;
            font-weight: bold;
            margin: 10px 0;
        }
        .stat-label {
            font-size: 14px;
            opacity: 0.9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Omega Watch WebSocket Client</h1>

        <div id="status" class="status disconnected">
            🔴 Disconnected
        </div>

        <div class="stats">
            <div class="stat-card">
                <div class="stat-label">Messages Sent</div>
                <div class="stat-value" id="sentCount">0</div>
            </div>
            <div class="stat-card">
                <div class="stat-label">Messages Received</div>
                <div class="stat-value" id="receivedCount">0</div>
            </div>
            <div class="stat-card">
                <div class="stat-label">Connection Time</div>
                <div class="stat-value" id="connectionTime">0s</div>
            </div>
        </div>

        <div class="controls">
            <button class="primary" onclick="connect()">Connect</button>
            <button class="secondary" onclick="disconnect()">Disconnect</button>
            <select id="watchSelect">
                <option value="">Select a watch...</option>
                <option value="speedmaster-moonwatch-1969">Speedmaster Moonwatch 1969</option>
                <option value="speedmaster-moonwatch-1957">Speedmaster CK2915 1957</option>
                <option value="seamaster-300-1957">Seamaster 300 1957</option>
                <option value="speedmaster-dark-side-moon-2013">Dark Side of the Moon</option>
                <option value="speedmaster-snoopy-2003">Snoopy Award</option>
            </select>
            <button class="primary" onclick="subscribeToWatch()">Subscribe</button>
            <button class="secondary" onclick="clearMessages()">Clear Log</button>
        </div>

        <h2>Message Log</h2>
        <div class="messages" id="messages"></div>
    </div>

    <script>
        let ws = null;
        let sentCount = 0;
        let receivedCount = 0;
        let connectionStartTime = null;
        let connectionTimer = null;

        function connect() {
            if (ws && ws.readyState === WebSocket.OPEN) {
                addMessage('Already connected', 'error');
                return;
            }

            addMessage('Connecting to ws://45.61.58.125:7600...', 'sent');

            ws = new WebSocket('ws://45.61.58.125:7600');

            ws.onopen = () => {
                updateStatus(true);
                addMessage('Connected to Omega Watch WebSocket server', 'received');
                connectionStartTime = Date.now();
                startConnectionTimer();
            };

            ws.onmessage = (event) => {
                receivedCount++;
                updateStats();

                const data = JSON.parse(event.data);
                addMessage(`Received: ${JSON.stringify(data, null, 2)}`, 'received');

                if (data.type === 'subscribed') {
                    addMessage(`Successfully subscribed to watch: ${data.watchId}`, 'received');
                }

                if (data.type === 'update') {
                    addMessage(`Update for ${data.watchId}: ${JSON.stringify(data.data)}`, 'received');
                }
            };

            ws.onerror = (error) => {
                addMessage(`WebSocket error: ${error}`, 'error');
            };

            ws.onclose = () => {
                updateStatus(false);
                addMessage('Disconnected from server', 'error');
                stopConnectionTimer();
            };
        }

        function disconnect() {
            if (ws) {
                ws.close();
                ws = null;
                updateStatus(false);
                addMessage('Disconnected by user', 'sent');
            }
        }

        function subscribeToWatch() {
            if (!ws || ws.readyState !== WebSocket.OPEN) {
                addMessage('Not connected. Please connect first.', 'error');
                return;
            }

            const watchId = document.getElementById('watchSelect').value;

            if (!watchId) {
                addMessage('Please select a watch first', 'error');
                return;
            }

            const message = {
                type: 'subscribe',
                watchId: watchId
            };

            ws.send(JSON.stringify(message));
            sentCount++;
            updateStats();

            addMessage(`Sent: ${JSON.stringify(message, null, 2)}`, 'sent');
        }

        function addMessage(text, type = '') {
            const messagesDiv = document.getElementById('messages');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${type}`;

            const timestamp = new Date().toLocaleTimeString();
            messageDiv.innerHTML = `
                <span class="timestamp">[${timestamp}]</span><br>
                ${text.replace(/\n/g, '<br>')}
            `;

            messagesDiv.appendChild(messageDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        }

        function clearMessages() {
            document.getElementById('messages').innerHTML = '';
            addMessage('Message log cleared', 'sent');
        }

        function updateStatus(connected) {
            const statusDiv = document.getElementById('status');

            if (connected) {
                statusDiv.className = 'status connected';
                statusDiv.textContent = '🟢 Connected';
            } else {
                statusDiv.className = 'status disconnected';
                statusDiv.textContent = '🔴 Disconnected';
            }
        }

        function updateStats() {
            document.getElementById('sentCount').textContent = sentCount;
            document.getElementById('receivedCount').textContent = receivedCount;
        }

        function startConnectionTimer() {
            connectionTimer = setInterval(() => {
                if (connectionStartTime) {
                    const elapsed = Math.floor((Date.now() - connectionStartTime) / 1000);
                    document.getElementById('connectionTime').textContent = `${elapsed}s`;
                }
            }, 1000);
        }

        function stopConnectionTimer() {
            if (connectionTimer) {
                clearInterval(connectionTimer);
                connectionTimer = null;
            }
            connectionStartTime = null;
            document.getElementById('connectionTime').textContent = '0s';
        }

        // Welcome message
        addMessage('Welcome to Omega Watch WebSocket Client! Click "Connect" to start.', 'received');
    </script>
</body>
</html>