space-browser / commits / 0b19b0a
fix: connect to same-origin /ws in a prod build
@@ -12,10 +12,16 @@ export type State = {
1212 players: Ship[];
1313 };
1414
15+// Dev: vite on :5173, game server on :8090. Prod: Caddy proxies /ws on the same
16+// origin, so the page's own scheme/host is the right socket.
17+const url = import.meta.env.DEV
18+ ? `ws://${location.hostname}:8090/ws`
19+ : `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/ws`;
20+
1521 export function connect(onState: (s: State) => void): { send: (msg: unknown) => void } {
1622 let ws: WebSocket | null = null;
1723 const open = () => {
18- ws = new WebSocket(`ws://${location.hostname}:8090/ws`);
24+ ws = new WebSocket(url);
1925 ws.onmessage = (e) => {
2026 const m = JSON.parse(e.data);
2127 if (m.t === 'state') onState(m);