From d7c2e8dc8583a4ebe05381d13eff6be17505a270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 4 Jan 2026 16:14:21 +0100 Subject: [PATCH] Dynamically generate WebSocket URLs based on current domain to support both HTTP and HTTPS environments. --- cmd/client/connection_wasm.go | 12 +++++++++--- cmd/client/web/game.js | 7 ++++++- 2 files changed, 15 insertions(+), 4 deletions(-) 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 = () => {