3.8 KiB
WebRTC Multiplayer Setup
The game now supports WebRTC multiplayer for WebAssembly (browser) builds!
How It Works
The NetworkManager automatically detects the platform:
- Native builds (Windows, Linux, Mac): Uses ENet (traditional UDP networking)
- Web builds: Uses WebRTC (peer-to-peer browser networking)
Important Note About WebRTC
WebRTC requires a signaling mechanism to establish connections between peers. This is different from ENet which uses direct IP:port connections.
Current Implementation Status
✅ Done:
- Auto-detection of web platform
- WebRTCMultiplayerPeer creation for host and client
- Basic connection setup
- STUN server configured (
stun:ruinborn.thefirstboss.com:3578) for NAT traversal
⚠️ Requires Additional Setup:
- Signaling server to exchange connection info between peers
- Session Description Protocol (SDP) offer/answer exchange
- ICE candidate exchange
Quick Setup Options
Option 1: Use a Public Signaling Server (Easiest)
Use a service like:
- peerjs-server
- Matchbox (Godot-specific)
- Roll your own simple WebSocket server
Option 2: Manual Connection (For Testing)
For local testing, you can manually exchange connection strings:
- Host creates a game
- Host copies their connection info
- Joiner pastes host's connection info
- Joiner generates their own connection info
- Host pastes joiner's connection info
Option 3: Implement Signaling Server (Recommended for Production)
Create a simple WebSocket signaling server that:
- Maintains a list of active lobbies
- Relays SDP offers/answers between peers
- Relays ICE candidates between peers
Example: Using Matchbox (Recommended)
Matchbox is a simple signaling server specifically for Godot WebRTC:
# Install matchbox server
cargo install matchbox_server
# Run the server
matchbox_server
Then update the code to connect to your matchbox server when in web mode.
Testing WebRTC Locally
-
Export for HTML5:
- Project -> Export -> Add -> HTML5 (WebAssembly)
- Export the project
-
Run a local web server:
# Python 3 python -m http.server 8000 # Or use any web server -
Open in browsers:
- Host:
http://localhost:8000 - Client:
http://localhost:8000(in another browser tab/window)
- Host:
-
Connect:
- One browser hosts
- Other browser joins
- (Requires signaling server to be running)
Alternative: WebSocket Multiplayer
If WebRTC setup is too complex, you can use WebSocketMultiplayerPeer instead:
# In network_manager.gd, replace WebRTCMultiplayerPeer with:
var peer = WebSocketMultiplayerPeer.new()
# For host:
peer.create_server(port)
# For client:
peer.create_client("ws://your-server:port")
WebSocket is simpler but requires a dedicated server (can't be P2P like WebRTC).
Production Deployment
For production, you'll need:
- A signaling server (for WebRTC) or game server (for WebSocket)
- HTTPS hosting (required for WebRTC in browsers)
- Proper CORS configuration
- STUN/TURN servers for NAT traversal (optional but recommended)
Current Status
The game will:
- ✅ Detect web platform automatically
- ✅ Create WebRTC peers
- ⚠️ Require signaling server setup to actually connect
To complete the setup, implement one of the options above based on your needs.
Quick Recommendation
For Testing: Use Matchbox (Option 3) For Production: Implement a proper signaling server with lobby/matchmaking