diff --git a/js/signature.js b/js/signature.js index ad01792..0676591 100644 --- a/js/signature.js +++ b/js/signature.js @@ -6,6 +6,7 @@ const ctx = canvas.getContext('2d'); let isDrawing = false; let hasSignature = false; let ws = null; +let shouldReconnect = true; let currentPdf = null; let pdfDoc = null; let currentPageNum = 1; @@ -45,6 +46,26 @@ function connectWebSocket() { if (warning) { warning.style.display = 'block'; } + } else if (data.type === 'replaced_by_new_station') { + console.log('⚠️ Diese Signatur-Station wurde durch eine neuere Verbindung ersetzt'); + + // Zeige eine deutliche Meldung an dieser Station + const warning = document.getElementById('alreadyConnectedWarning'); + if (warning) { + warning.textContent = currentLanguage === 'de' + ? 'Diese Signatur-Station wurde von einer neueren Station abgelöst.' + : 'This signature station has been replaced by a newer station.'; + warning.style.display = 'block'; + } + + // Nicht automatisch neu verbinden, um Ping-Pong zu vermeiden + shouldReconnect = false; + + try { + ws.close(); + } catch (error) { + console.error('Fehler beim Schließen der WebSocket-Verbindung nach Ersatz:', error); + } } else if (data.type === 'pdf') { console.log('PDF empfangen'); currentPdf = new Uint8Array(data.pdf); @@ -113,7 +134,11 @@ function connectWebSocket() { ws.onclose = () => { console.log('WebSocket getrennt'); - setTimeout(connectWebSocket, 1000); + if (shouldReconnect) { + setTimeout(connectWebSocket, 1000); + } else { + console.log('Automatischer Reconnect ist deaktiviert (Station wurde ersetzt)'); + } }; } diff --git a/server.js b/server.js index f7e61b6..4b91523 100644 --- a/server.js +++ b/server.js @@ -205,22 +205,37 @@ function handleMasterRegister(ws) { } function handleSignatureRegister(ws) { - // Check if another signature station is already connected + // Wenn bereits eine Signatur-Station verbunden ist, erhält die NEUE Station Priorität. if (signatureConnection && signatureConnection.readyState === WebSocket.OPEN) { - // Inform the new station that another station is already connected - ws.send(JSON.stringify({ - type: 'already_connected' - })); - console.log('⚠️ Neue Signatur-Station versucht sich zu verbinden, aber bereits eine Station verbunden'); - console.log('⚠️ Neue Station wurde informiert, alte Station bleibt aktiv'); - // Keep the old connection, don't replace it - // Don't register the new connection as signatureConnection - return; // Exit without registering the new connection - } + console.log('⚠️ Neue Signatur-Station meldet sich an – bestehende Station wird abgemeldet'); - // No existing connection, register this new one - signatureConnection = ws; - console.log('Signatur-Station registriert'); + // Informiere die bisher aktive Station, dass sie durch eine neue ersetzt wird + try { + signatureConnection.send(JSON.stringify({ + type: 'replaced_by_new_station' + })); + console.log('Aktive Signatur-Station über neue Station informiert'); + } catch (error) { + console.error('Fehler beim Informieren der alten Signatur-Station:', error); + } + + // Alte Verbindung merken und neue sofort als aktive Station setzen + const oldConnection = signatureConnection; + signatureConnection = ws; + console.log('Signatur-Station gewechselt: neue Verbindung ist nun aktiv'); + + // Alte Verbindung sauber schließen (dieser Close-Event betrifft NICHT die neue Station) + try { + oldConnection.close(); + console.log('Alte Signatur-Station Verbindung geschlossen'); + } catch (error) { + console.error('Fehler beim Schließen der alten Signatur-Station:', error); + } + } else { + // Keine bestehende Station – neue Verbindung einfach registrieren + signatureConnection = ws; + console.log('Signatur-Station registriert'); + } // Notify master that signature station is now connected if (masterConnection && masterConnection.readyState === WebSocket.OPEN) {