pub trait ServerHandler:
Send
+ Sync
+ 'static {
// Provided methods
fn on_connect(&self, _addr: SocketAddr) -> bool { ... }
fn on_join(
&self,
_client_id: Uuid,
_room_id: &str,
_addr: SocketAddr,
_data: &[u8],
) -> (bool, Option<String>) { ... }
fn on_leave(&self, _client_id: Uuid, _room_id: &str) { ... }
fn on_room_create(&self, _room_id: &str) { ... }
fn on_room_delete(&self, _room_id: &str) { ... }
fn on_broadcast(&self, _client_id: Uuid, _room_id: &str, _data: &[u8]) { ... }
fn on_backpressure(&self, _client_id: Uuid, _room_id: &str) { ... }
fn on_shutdown(&self) { ... }
}Expand description
Optional hook trait for server lifecycle events. All methods have no-op defaults — implement only what you need.
§Composability
These hooks are the extension point for game logic. The library never interprets room metadata or makes game-specific decisions. Use the hooks to implement private rooms, max player limits, matchmaking, bans, etc.
§Security
Use on_connect to filter connections by address
before they’re fully accepted.
§Backpressure
When a client’s write channel is full, the frame is dropped and
on_backpressure is called. This is
intentional for real-time game relay — a slow client shouldn’t stall the server.
Provided Methods§
Sourcefn on_connect(&self, _addr: SocketAddr) -> bool
fn on_connect(&self, _addr: SocketAddr) -> bool
Called when a new TCP connection arrives. Return false to reject it.
Called before any resources are allocated for the connection.
Sourcefn on_join(
&self,
_client_id: Uuid,
_room_id: &str,
_addr: SocketAddr,
_data: &[u8],
) -> (bool, Option<String>)
fn on_join( &self, _client_id: Uuid, _room_id: &str, _addr: SocketAddr, _data: &[u8], ) -> (bool, Option<String>)
Called when a client requests to join a room.
Return (true, _) to allow the join.
Return (false, Some(reason)) to reject with a custom error message.
Return (false, None) to reject with the default “join rejected” message.
Use this to implement private rooms (check password), max player limits (check client count) or use payload_data field to validate any sort of tokens, bans (check addr), add room_limit or any custom join logic you wish
Sourcefn on_leave(&self, _client_id: Uuid, _room_id: &str)
fn on_leave(&self, _client_id: Uuid, _room_id: &str)
Called when a client leaves a room (or disconnects).
Sourcefn on_room_create(&self, _room_id: &str)
fn on_room_create(&self, _room_id: &str)
Called after a room is created (including via ServerHandle::create_room_runtime).
Sourcefn on_room_delete(&self, _room_id: &str)
fn on_room_delete(&self, _room_id: &str)
Called after a room is deleted.
Sourcefn on_broadcast(&self, _client_id: Uuid, _room_id: &str, _data: &[u8])
fn on_broadcast(&self, _client_id: Uuid, _room_id: &str, _data: &[u8])
Called when a broadcast message is relayed. client_id is the sender, and the message has already been relayed to all peers.
Sourcefn on_backpressure(&self, _client_id: Uuid, _room_id: &str)
fn on_backpressure(&self, _client_id: Uuid, _room_id: &str)
Called when a frame is dropped because a client’s write channel is full. The affected clients are identified by their UUIDs.
NOTE: For auto-kicking or pausing slow clients, you need to monitor channel length via ServerHandle::get_client_channel_len
or ServerHandle::get_room_channel_lens and implement your own logic in this hook.
The library does not auto-kick or pause clients on backpressure, as some games may prefer to drop frames silently.
Sourcefn on_shutdown(&self)
fn on_shutdown(&self)
Called when server is shutdown via ctrl-c (shutdown signals) Can be used as a graceful shutdown signal for any background tasks or post-shutdown cleanup or jobs