Skip to main content

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

Initiates matchmaking for a new game.
Example:

Cancels active matchmaking request.
Example:

JOIN_GAME

Joins an existing game by ID (used after GAME_FOUND).
Example:

JOIN_ROOM

Joins a private room (for custom games with friends).
Example:

PLAY_MOVE

Submits a move to the server.
Example:
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).
Example:

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

Cause: Attempting to play on a non-empty cell.

User Status States

Server tracks user status to enforce valid transitions:

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:
The UI should handle this by:
  1. Displaying a “Connection Lost” message
  2. Attempting to reconnect
  3. Restoring game state if reconnection succeeds

Security Considerations

Server-side validation is critical:
  • Always verify it’s the correct player’s turn
  • Validate move coordinates are within bounds
  • Check that target cell is empty
  • Authenticate user identity before accepting moves
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