This commit is contained in:
team3
2026-07-10 17:56:41 +02:00
parent ab1a28f0e7
commit 14bcf1b8e9
6 changed files with 59 additions and 7 deletions

View File

@@ -34,10 +34,18 @@ export const api = {
}
export function wsVerbinden(onEvent) {
const url = (import.meta.env.DEV ? 'ws://localhost:8000' : `ws://${location.host}`) + '/ws'
// Protokoll an die Seite koppeln: HTTPS erlaubt nur wss:// (Mixed Content)
const wsProto = location.protocol === 'https:' ? 'wss' : 'ws'
const url = (import.meta.env.DEV ? 'ws://localhost:8000' : `${wsProto}://${location.host}`) + '/ws'
let ws
const verbinden = () => {
ws = new WebSocket(url)
try {
ws = new WebSocket(url)
} catch (e) {
// nie den Aufrufer mitreißen (der SecurityError killte sonst den Poll)
console.error('WebSocket nicht verfügbar:', e)
return
}
ws.onopen = () => onEvent({ typ: 'verbunden' }) // nach Reconnect: Stand neu laden
ws.onmessage = (e) => onEvent(JSON.parse(e.data))
ws.onclose = () => setTimeout(verbinden, 2000)