173 lines
4.1 KiB
Markdown
173 lines
4.1 KiB
Markdown
# WebRTC Multiplayer Guide
|
|
|
|
## How WebRTC Multiplayer Works
|
|
|
|
### Architecture
|
|
|
|
Your game now supports two networking modes:
|
|
|
|
1. **ENet (Native builds)**: Windows/Linux/Mac players connect via IP:Port
|
|
2. **WebRTC (Web builds)**: Browser players connect via Room IDs
|
|
|
|
**Important**: ENet and WebRTC are NOT compatible with each other!
|
|
- PC (ENet) players can only play with other PC (ENet) players
|
|
- Browser (WebRTC) players can only play with other Browser (WebRTC) players
|
|
|
|
### Your Server Setup
|
|
|
|
- **Matchbox Signaling**: `ws://ruinborn.thefirstboss.com:3536`
|
|
- **STUN Server**: `stun:ruinborn.thefirstboss.com:3578`
|
|
|
|
## How Players Find Each Other
|
|
|
|
### WebRTC/Browser Mode (Room-Based)
|
|
|
|
1. **Host creates a game**:
|
|
- Clicks "Host Game"
|
|
- Game generates a 6-character **Room Code** (e.g., "ABC123")
|
|
- Share this code with friends
|
|
|
|
2. **Players join**:
|
|
- Enter the **Room Code** in the "Join Game" field
|
|
- Click "Join"
|
|
- Matchbox connects all players in the same room
|
|
|
|
### Example Flow
|
|
|
|
```
|
|
Host (Browser):
|
|
- Clicks "Host"
|
|
- Gets Room Code: "XYZ789"
|
|
- Tells friend: "Join room XYZ789"
|
|
|
|
Player (Browser):
|
|
- Types "XYZ789" in join field
|
|
- Clicks "Join"
|
|
- Connected!
|
|
```
|
|
|
|
## Testing Scenarios
|
|
|
|
### Scenario 1: Browser to Browser (WebRTC)
|
|
✅ **Works!**
|
|
```
|
|
1. Open game in Chrome: host.com/game
|
|
2. Host creates game → Gets code "ABC123"
|
|
3. Open game in Firefox: host.com/game
|
|
4. Join with code "ABC123"
|
|
5. Both connected via WebRTC!
|
|
```
|
|
|
|
### Scenario 2: PC to Browser (Mixed)
|
|
❌ **Doesn't Work!**
|
|
```
|
|
PC (ENet) ←→ Browser (WebRTC) = INCOMPATIBLE
|
|
```
|
|
|
|
### Scenario 3: Phone Browser to PC Browser (WebRTC)
|
|
✅ **Works!**
|
|
```
|
|
1. PC Browser hosts → code "XYZ456"
|
|
2. Phone browser joins "XYZ456"
|
|
3. Connected via WebRTC!
|
|
```
|
|
|
|
### Scenario 4: PC to PC (ENet)
|
|
✅ **Works!**
|
|
```
|
|
1. PC1 hosts → Shows IP: 192.168.1.100
|
|
2. PC2 joins → Enters "192.168.1.100"
|
|
3. Connected via ENet!
|
|
```
|
|
|
|
## Can PC Host and Browser Join?
|
|
|
|
**Short Answer**: No, not with the current setup.
|
|
|
|
**Long Answer**: ENet (PC) and WebRTC (Browser) use completely different protocols. They can't talk to each other directly.
|
|
|
|
**Solution**: If you want cross-platform play, you have two options:
|
|
|
|
### Option A: Make PC Use WebRTC Too
|
|
|
|
Update PC builds to also use WebRTC when you want to play with browser users:
|
|
|
|
```gdscript
|
|
# Force WebRTC mode even on native builds
|
|
use_webrtc = true # in network_manager.gd
|
|
```
|
|
|
|
Then PC and Browser can use the same room codes!
|
|
|
|
### Option B: Use a Dedicated Server
|
|
|
|
Run a dedicated server that:
|
|
- Accepts both ENet and WebRTC connections
|
|
- Bridges between them
|
|
|
|
This is more complex but allows true cross-platform play.
|
|
|
|
## Room Codes
|
|
|
|
Room codes are automatically generated as 6-character codes:
|
|
- Easy to share (just text it!)
|
|
- No IP addresses to remember
|
|
- Works through NATs/firewalls
|
|
|
|
Example codes: `ABC123`, `XYZ789`, `PLAYER`, `GAME42`
|
|
|
|
## Exporting for Web
|
|
|
|
1. **Export HTML5**:
|
|
- Project → Export → Add → HTML5 (WebAssembly)
|
|
- Export the project
|
|
|
|
2. **Host on Web Server**:
|
|
```bash
|
|
# Your files
|
|
python -m http.server 8000
|
|
```
|
|
|
|
3. **Players Access**:
|
|
- `http://yoursite.com/game`
|
|
- Works on PC browsers, phone browsers, tablets!
|
|
|
|
## Security Notes
|
|
|
|
- Room codes are NOT secure (anyone with code can join)
|
|
- For production, add:
|
|
- Password-protected rooms
|
|
- Room capacity limits
|
|
- Ban/kick functionality
|
|
- Room expiration
|
|
|
|
## Troubleshooting
|
|
|
|
**"Connection failed" in browser:**
|
|
- Check matchbox server is running: `netstat -tulpn | grep 3536`
|
|
- Check firewall allows port 3536
|
|
- Check browser console for errors
|
|
|
|
**"Can't see other players":**
|
|
- Make sure both players entered the SAME room code
|
|
- Check both are using browser builds (not mixing PC/browser)
|
|
|
|
**"Stuck on connecting":**
|
|
- STUN server must be reachable
|
|
- Check port 3578 is open
|
|
- Try different browsers
|
|
|
|
## Current Limitations
|
|
|
|
1. PC (ENet) ←/→ Browser (WebRTC) can't play together
|
|
2. Need to share room codes manually (no lobby browser yet)
|
|
3. No reconnection if disconnected (must rejoin)
|
|
|
|
## Next Steps
|
|
|
|
To improve multiplayer:
|
|
1. Add lobby browser (see active rooms)
|
|
2. Add friend codes/invites
|
|
3. Add voice chat (WebRTC supports it!)
|
|
4. Add dedicated server for PC↔Browser bridge
|