diff --git a/cmd/client/connection_wasm.go b/cmd/client/connection_wasm.go index e3a30c3..010ef64 100644 --- a/cmd/client/connection_wasm.go +++ b/cmd/client/connection_wasm.go @@ -20,14 +20,20 @@ type WebSocketMessage struct { // wsConn verwaltet die WebSocket-Verbindung im Browser type wsConn struct { - ws js.Value + ws js.Value messagesChan chan []byte - connected bool + connected bool } // connectToServer verbindet sich รผber WebSocket mit dem Gateway func (g *Game) connectToServer() { - serverURL := "ws://localhost:8080/ws" + // Automatisch die richtige WebSocket-URL basierend auf der aktuellen Domain + protocol := "ws:" + if js.Global().Get("location").Get("protocol").String() == "https:" { + protocol = "wss:" + } + host := js.Global().Get("location").Get("host").String() + serverURL := protocol + "//" + host + "/ws" log.Printf("๐Ÿ”Œ Verbinde zu WebSocket-Gateway: %s", serverURL) ws := js.Global().Get("WebSocket").New(serverURL) diff --git a/cmd/client/web/game.js b/cmd/client/web/game.js index 0807719..32a6be8 100644 --- a/cmd/client/web/game.js +++ b/cmd/client/web/game.js @@ -136,7 +136,12 @@ function connectLeaderboardWebSocket() { return; // Already connected } - const wsURL = 'ws://localhost:8080/ws'; + // Automatisch die richtige WebSocket-URL basierend auf der aktuellen Domain + const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; + const host = window.location.host; + const wsURL = `${protocol}//${host}/ws`; + + console.log('๐Ÿ”Œ Verbinde zu WebSocket:', wsURL); leaderboardWS = new WebSocket(wsURL); leaderboardWS.onopen = () => {