|
| 1 | +# GitHub Pages + Local Server Setup |
| 2 | + |
| 3 | +## Problem |
| 4 | +When hosting the client on GitHub Pages (HTTPS), browsers block insecure WebSocket connections (ws://) due to mixed content security policies. To connect to a local server, you need secure WebSocket connections (wss://). |
| 5 | + |
| 6 | +## Solution Overview |
| 7 | +The client now automatically detects the page protocol and uses: |
| 8 | +- `wss://` when loaded from HTTPS (GitHub Pages) |
| 9 | +- `ws://` when loaded from HTTP (local development) |
| 10 | + |
| 11 | +## Setup Options |
| 12 | + |
| 13 | +### Option 1: Use a Reverse Proxy with SSL (Recommended for LAN) |
| 14 | + |
| 15 | +Use a reverse proxy like **ngrok**, **Cloudflare Tunnel**, or **nginx** to provide SSL termination for your local server. |
| 16 | + |
| 17 | +#### Using ngrok (Easiest) |
| 18 | + |
| 19 | +1. **Install ngrok**: Download from https://ngrok.com/download |
| 20 | + |
| 21 | +2. **Start your game server** (port 8080) |
| 22 | + ```bash |
| 23 | + cd server |
| 24 | + mvn clean compile exec:java |
| 25 | + ``` |
| 26 | + |
| 27 | +3. **Start ngrok tunnel**: |
| 28 | + ```bash |
| 29 | + ngrok http 8080 |
| 30 | + ``` |
| 31 | + |
| 32 | +4. **Connect from GitHub Pages**: |
| 33 | + - ngrok will give you a URL like: `https://abc123.ngrok.io` |
| 34 | + - Enter in the game: `abc123.ngrok.io` (without https://) |
| 35 | + - The client will automatically use `wss://abc123.ngrok.io:8080/game` |
| 36 | + |
| 37 | +**Note**: Free ngrok URLs change each restart. Get a free static domain at ngrok.com |
| 38 | + |
| 39 | +#### Using Cloudflare Tunnel (Free Static URL) |
| 40 | + |
| 41 | +1. **Install cloudflared**: https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/ |
| 42 | + |
| 43 | +2. **Start tunnel**: |
| 44 | + ```bash |
| 45 | + cloudflared tunnel --url http://localhost:8080 |
| 46 | + ``` |
| 47 | + |
| 48 | +3. Use the provided URL in your game client |
| 49 | + |
| 50 | +### Option 2: Self-Signed Certificate (For Testing Only) |
| 51 | + |
| 52 | +⚠️ **Warning**: Browsers will show security warnings. Not recommended for production. |
| 53 | + |
| 54 | +1. **Generate certificate**: |
| 55 | + ```bash |
| 56 | + keytool -genkeypair -keyalg RSA -keysize 2048 -keystore keystore.jks \ |
| 57 | + -alias minitank -validity 365 -storepass changeit |
| 58 | + ``` |
| 59 | + |
| 60 | +2. **Modify GameServer.java** to use SSLServerSocket |
| 61 | + |
| 62 | +3. **Accept certificate** in browser (visit https://your-lan-ip:8080 first) |
| 63 | + |
| 64 | +### Option 3: Local Development Only |
| 65 | + |
| 66 | +For local testing without HTTPS: |
| 67 | + |
| 68 | +1. **Host client locally** (not on GitHub Pages): |
| 69 | + ```bash |
| 70 | + cd client |
| 71 | + python -m http.server 3000 |
| 72 | + ``` |
| 73 | + |
| 74 | +2. **Access via HTTP**: http://localhost:3000 |
| 75 | + |
| 76 | +3. **Client will use**: `ws://` protocol automatically |
| 77 | + |
| 78 | +## Current Configuration |
| 79 | + |
| 80 | +The client (`network-manager.js`) now includes: |
| 81 | + |
| 82 | +```javascript |
| 83 | +getWebSocketProtocol() { |
| 84 | + return window.location.protocol === 'https:' ? 'wss:' : 'ws:'; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +This ensures the correct protocol is used based on how the page is loaded. |
| 89 | + |
| 90 | +## Recommended Setup for LAN Gaming with GitHub Pages |
| 91 | + |
| 92 | +1. **Use ngrok or Cloudflare Tunnel** for SSL termination |
| 93 | +2. **Share the tunnel URL** with other players on your LAN |
| 94 | +3. **Everyone connects** via GitHub Pages using the tunnel hostname |
| 95 | +4. **Advantage**: No certificate warnings, works from any network |
| 96 | + |
| 97 | +## Testing |
| 98 | + |
| 99 | +### Test Local Connection: |
| 100 | +1. Start server: `make run-server` |
| 101 | +2. Open: http://localhost:8080 (or use index.html locally) |
| 102 | +3. Enter server: `localhost` |
| 103 | +4. Should connect via `ws://localhost:8080/game` |
| 104 | + |
| 105 | +### Test GitHub Pages Connection: |
| 106 | +1. Start server with ngrok: `ngrok http 8080` |
| 107 | +2. Open: https://th33k.github.io/minitankfire.game/ |
| 108 | +3. Enter server: `abc123.ngrok.io` (your ngrok domain) |
| 109 | +4. Should connect via `wss://abc123.ngrok.io:8080/game` |
| 110 | + |
| 111 | +## Port Considerations |
| 112 | + |
| 113 | +The server runs on port **8080**. When using a tunnel service: |
| 114 | +- The tunnel handles SSL on the public side |
| 115 | +- Forwards to your local server on port 8080 |
| 116 | +- You only need to enter the tunnel hostname (e.g., `abc123.ngrok.io`) |
| 117 | +- Don't include the port in the server address field |
| 118 | +- The client automatically appends `:8080/game` |
| 119 | + |
| 120 | +## Troubleshooting |
| 121 | + |
| 122 | +### "Mixed Content" Error |
| 123 | +- ✅ Fixed: Client now uses correct protocol |
| 124 | +- If still occurring: clear browser cache |
| 125 | + |
| 126 | +### "Connection Refused" |
| 127 | +- Check server is running: `netstat -an | findstr 8080` |
| 128 | +- Verify firewall allows port 8080 |
| 129 | +- For tunnels: check tunnel is active |
| 130 | + |
| 131 | +### Certificate Warnings |
| 132 | +- With ngrok/Cloudflare: No warnings (they provide valid certs) |
| 133 | +- With self-signed: Expected, must accept in browser |
| 134 | + |
| 135 | +### Can't Connect from Other Devices |
| 136 | +- Use tunnel service (ngrok/Cloudflare) |
| 137 | +- OR ensure your LAN IP is accessible and firewall allows connections |
0 commit comments