Overview
Showdown Trivia uses WebSocket connections to deliver a fully real-time multiplayer trivia experience. Players receive questions simultaneously, submit answers in real-time, and see live score updates throughout the game.How It Works
WebSocket Connection
The game establishes persistent WebSocket connections between clients and the server using the Gorilla WebSocket library. Each player maintains an active connection throughout the game session.Connection Settings
- Read Buffer: 1024 bytes
- Write Buffer: 1024 bytes
- Read Limit: 512 bytes per message
- Ping Interval: 9 seconds
- Pong Wait: 10 seconds
internal/web/ws/hub.go:17-21, internal/web/ws/client.go:14-16
Game Flow
When a player creates or joins a game room:- Connection Established - WebSocket connection upgraded from HTTP at
/wscreateor/wsjoin/{id} - Client Registration - Player added to room’s client list with username
- Game Start - Room owner triggers game start with
start_gameevent - Question Delivery - Questions sent sequentially to all connected players
- Answer Collection - Players submit answers via
send_answerevent - Score Calculation - Correct answers scored when timer expires
- Winner Announcement - Final scores displayed when all questions completed
Message Types
The WebSocket implementation supports several event types for game interaction:Client → Server Events
start_game
start_game
Initiates the game session. Only available to the room owner.Handler:
internal/web/ws/client.go:78-84send_answer
send_answer
Submits a player’s answer to the current question.Handler:
internal/web/ws/client.go:85-93Server → Client Messages
question
question
Broadcasts a new trivia question to all players in the room.Payload: Question object with text, options, current question number, total questions, and timerHandler:
internal/web/ws/rooms.go:44-53info
info
Sends informational messages during gameplay.Handler:
internal/web/ws/rooms.go:54-63game_end
game_end
Announces game completion with final scores for all players.Payload: Map of usernames to scoresHandler:
internal/web/ws/rooms.go:64-74Connection Management
Keep-Alive Mechanism
The WebSocket connection uses ping/pong frames to detect disconnected clients:- Server sends ping every 9 seconds
- Client must respond with pong within 10 seconds
- Connection closed if pong not received in time
internal/web/ws/client.go:107-128
Concurrent Read/Write
Each client runs two goroutines:- Read goroutine - Listens for incoming messages from client
- Write goroutine - Sends messages to client from egress channel
internal/web/ws/client.go:46-98, internal/web/ws/client.go:100-131
Game Timing
Each question has a configurable timer set by the room creator:- Minimum timer: 2 seconds
- Maximum timer: 20 seconds
- Default: Configurable per game
- Answer submission closes for that question
- Correct answers are scored
- Next question begins (or game ends)
internal/web/ws/rooms.go:31-32, internal/core/game/game.go:57-83
Broadcasting
Messages are broadcast to all players in a room simultaneously:internal/web/ws/rooms.go:22-29
The real-time gameplay system is designed to handle multiple concurrent games, each in isolated rooms with independent WebSocket connections.
Error Handling
The system gracefully handles various error conditions:- Unexpected close errors - Client removed from room, connection cleaned up
- Invalid JSON - Error message sent to client
- Read/Write failures - Client disconnected automatically
- Room not found - Connection rejected before upgrade
internal/web/ws/client.go:36-45, internal/web/ws/client.go:61-68