Private
Public Access
1
0

Dynamically generate WebSocket URLs based on current domain to support both HTTP and HTTPS environments.
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 2m24s

This commit is contained in:
Sebastian Unterschütz
2026-01-04 16:14:21 +01:00
parent fa59070c4a
commit d7c2e8dc85
2 changed files with 15 additions and 4 deletions

View File

@@ -20,14 +20,20 @@ type WebSocketMessage struct {
// wsConn verwaltet die WebSocket-Verbindung im Browser // wsConn verwaltet die WebSocket-Verbindung im Browser
type wsConn struct { type wsConn struct {
ws js.Value ws js.Value
messagesChan chan []byte messagesChan chan []byte
connected bool connected bool
} }
// connectToServer verbindet sich über WebSocket mit dem Gateway // connectToServer verbindet sich über WebSocket mit dem Gateway
func (g *Game) connectToServer() { 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) log.Printf("🔌 Verbinde zu WebSocket-Gateway: %s", serverURL)
ws := js.Global().Get("WebSocket").New(serverURL) ws := js.Global().Get("WebSocket").New(serverURL)

View File

@@ -136,7 +136,12 @@ function connectLeaderboardWebSocket() {
return; // Already connected 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 = new WebSocket(wsURL);
leaderboardWS.onopen = () => { leaderboardWS.onopen = () => {