Overview
BeeHex uses WebSocket for bidirectional real-time communication between clients and the game server. The protocol is packet-based with JSON serialization.Server Endpoint:
ws://{IP_HOST}:3002/All packets are JSON-encoded with a type discriminator field.Connection Lifecycle
WebsocketHandler Class
Location:src/app/game_mode/WebsocketHandler.ts
The client-side handler manages WebSocket connection and packet routing.
Sending Packets
Handling Packets
Packet Types
Location:src/app/definitions.ts
Server-Bound Packets
Packets sent from client to server:Client-Bound Packets
Packets sent from server to client:Packet Specifications
- Server-Bound
- Client-Bound
GAME_SEARCH
Initiates matchmaking for a new game.CANCEL_GAME_SEARCH
Cancels active matchmaking request.JOIN_GAME
Joins an existing game by ID (used after GAME_FOUND).JOIN_ROOM
Joins a private room (for custom games with friends).PLAY_MOVE
Submits a move to the server.The server validates that:
- It’s the player’s turn
- The cell at (y, x) is empty
- The game is still in progress
FORFEIT_GAME
Forfeits the current game (instant loss).Game Flow Example
Here’s a complete example of a multiplayer game session:1
Player connects and searches
Client → Server:Server → Client:
2
Match found
Server → Client:
3
Join game
Client → Server:Server → Client:
4
First player makes move
Client (Alice) → Server:Server → Both Clients:
5
Second player responds
Client (Bob) → Server:Server → Both Clients:
6
Game continues...
Players alternate moves until one player connects their edges.
7
Game ends
Server → Both Clients:
Error Handling
The server sends error messages for invalid operations:Common Errors
- Invalid Move
- Wrong Turn
- Game Not Found
- Already in Game
User Status States
Database Game Structure
Games are persisted in the database with the following schema:The
moves field uses algebraic notation where each move is encoded as a grid position.Connection Management
Awaiting Connection
Graceful Disconnection
When the WebSocket closes, the client is notified:- Displaying a “Connection Lost” message
- Attempting to reconnect
- Restoring game state if reconnection succeeds
Security Considerations
The client-side code does NOT perform authentication - this must be handled by the server through session tokens or similar mechanisms.Next Steps
Architecture Overview
Return to high-level architecture documentation
Game Engine
Explore AI algorithms and game state management