Skip to content

Commit edb6bf3

Browse files
committed
feat: add favicon and improve WebSocket connection handling
1 parent 05a139c commit edb6bf3

7 files changed

Lines changed: 555 additions & 2 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ python -m http.server 3000
9595

9696
**Open Browser**: `http://localhost:3000`
9797

98+
### Using GitHub Pages + Local Server
99+
100+
**Play Online**: https://th33k.github.io/minitankfire.game/
101+
102+
**Setup Local Server with SSL Tunnel** (Required for HTTPS):
103+
104+
1. **Install ngrok**: https://ngrok.com/download
105+
106+
2. **Start Server**:
107+
```bash
108+
make run-server
109+
```
110+
111+
3. **Start Tunnel** (new terminal):
112+
```bash
113+
ngrok http 8080
114+
```
115+
116+
4. **Connect**: Enter your ngrok URL (e.g., `abc123.ngrok.io`) in the game
117+
118+
**See [GITHUB_PAGES_SETUP.md](docs/GITHUB_PAGES_SETUP.md) for detailed instructions**
119+
98120
---
99121

100122
## Prerequisites

client/favicon.svg

Lines changed: 34 additions & 0 deletions
Loading

client/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta name="description" content="Tank Arena - Real-time multiplayer tank battle arena game">
77
<meta name="theme-color" content="#00ff88">
88
<title>Tank Arena: Online</title>
9+
<link rel="icon" type="image/svg+xml" href="favicon.svg">
910
<link rel="stylesheet" href="css/style.css">
1011
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
1112
<link rel="preconnect" href="https://cdnjs.cloudflare.com">

client/js/managers/network-manager.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ export class NetworkManager {
99
this.currentPing = 0;
1010
}
1111

12+
// Determine the correct WebSocket protocol based on page protocol
13+
getWebSocketProtocol() {
14+
return window.location.protocol === 'https:' ? 'wss:' : 'ws:';
15+
}
16+
1217
connectToLobby(serverAddress) {
1318
try {
14-
this.lobbyWs = new WebSocket(`ws://${serverAddress}:8080/game`);
19+
const protocol = this.getWebSocketProtocol();
20+
this.lobbyWs = new WebSocket(`${protocol}//${serverAddress}:8080/game`);
1521

1622
this.lobbyWs.onopen = () => {
1723
this.sendLobbyMessage({ type: 'lobby_info' });
@@ -48,7 +54,8 @@ export class NetworkManager {
4854
}
4955

5056
connectToGame(name, serverAddress, onOpen, onMessage, onClose, onError) {
51-
const wsUrl = `ws://${serverAddress}:8080/game`;
57+
const protocol = this.getWebSocketProtocol();
58+
const wsUrl = `${protocol}//${serverAddress}:8080/game`;
5259
console.log('Attempting to connect to:', wsUrl);
5360

5461
this.ws = new WebSocket(wsUrl);

docs/dev/GITHUB_PAGES_SETUP.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# 🚀 Quick Setup: GitHub Pages + Local Server
2+
3+
## The Problem
4+
✗ GitHub Pages uses HTTPS
5+
✗ Your server uses WS (insecure WebSocket)
6+
✗ Browsers block mixed content (HTTPS → WS)
7+
8+
## The Solution
9+
✓ Use WSS (secure WebSocket) with a tunnel service
10+
✓ Client auto-detects and uses correct protocol
11+
12+
---
13+
14+
## 3-Step Setup with ngrok (Easiest)
15+
16+
### 1. Download & Install ngrok
17+
https://ngrok.com/download
18+
19+
### 2. Start Your Server
20+
```bash
21+
cd server
22+
mvn clean compile exec:java
23+
```
24+
25+
### 3. Start ngrok Tunnel
26+
```bash
27+
ngrok http 8080
28+
```
29+
30+
**You'll see:**
31+
```
32+
Forwarding https://abc123.ngrok.io -> http://localhost:8080
33+
```
34+
35+
---
36+
37+
## Connect & Play
38+
39+
### From GitHub Pages:
40+
1. Open: **https://th33k.github.io/minitankfire.game/**
41+
2. Enter server: **abc123.ngrok.io** (your ngrok URL)
42+
3. Click Join Game!
43+
44+
### Share with Friends:
45+
- Give them the ngrok URL: `abc123.ngrok.io`
46+
- They visit: https://th33k.github.io/minitankfire.game/
47+
- Everyone plays together!
48+
49+
---
50+
51+
## What Changed?
52+
53+
### ✅ Client (network-manager.js)
54+
```javascript
55+
// Auto-detects protocol
56+
getWebSocketProtocol() {
57+
return window.location.protocol === 'https:' ? 'wss:' : 'ws:';
58+
}
59+
60+
// Uses correct protocol
61+
const protocol = this.getWebSocketProtocol();
62+
this.ws = new WebSocket(`${protocol}//${serverAddress}:8080/game`);
63+
```
64+
65+
**Result:**
66+
- HTTPS page → Uses `wss://`
67+
- HTTP page → Uses `ws://`
68+
69+
---
70+
71+
## Alternative: Cloudflare Tunnel (Free Forever)
72+
73+
```bash
74+
# Install
75+
npm install -g cloudflared
76+
77+
# Run tunnel
78+
cloudflared tunnel --url http://localhost:8080
79+
```
80+
81+
**Advantage:** Free static URL that doesn't change
82+
83+
---
84+
85+
## Local Development (No Tunnel Needed)
86+
87+
Host client locally instead of GitHub Pages:
88+
89+
```bash
90+
cd client
91+
python -m http.server 3000
92+
```
93+
94+
Open: **http://localhost:3000**
95+
Server: **localhost**
96+
97+
Uses `ws://` automatically since page is HTTP.
98+
99+
---
100+
101+
## Troubleshooting
102+
103+
### Still getting "Mixed Content" errors?
104+
- Clear browser cache (Ctrl+Shift+Delete)
105+
- Hard refresh (Ctrl+F5)
106+
- Check console: should show `wss://` not `ws://`
107+
108+
### ngrok URL keeps changing?
109+
- Free plan gives new URL on restart
110+
- Get free static domain: https://ngrok.com/pricing (forever free tier)
111+
- OR use Cloudflare Tunnel
112+
113+
### Connection fails?
114+
- Check server is running: `netstat -an | findstr 8080`
115+
- Check ngrok is running and shows "online"
116+
- Try URL in browser: `https://abc123.ngrok.io` (should show some response)
117+
118+
### Firewall blocking?
119+
- ngrok bypasses firewall (tunnels through HTTPS)
120+
- Allow Java through Windows Firewall if needed
121+
122+
---
123+
124+
## Files Changed
125+
126+
`client/js/managers/network-manager.js` - Auto protocol detection
127+
`client/index.html` - Added favicon
128+
`client/favicon.svg` - New icon (no more 404)
129+
130+
---
131+
132+
## For More Details
133+
134+
📖 [Full Setup Guide](GITHUB_PAGES_SETUP.md)
135+
📖 [Development Guide](DEVELOPMENT.md)
136+
📖 [Architecture](ARCHITECTURE.md)

0 commit comments

Comments
 (0)