session_manager module
Session Manager
Orchestrates the full multiplayer host/client lifecycle.
Runs an asyncio event loop in a background daemon thread and uses
Qt signals (pyqtSignal) to safely communicate from
async callbacks back to the main UI thread.
In host mode the manager spins up a
WebSocketServer,
SessionHost, and the
EventBridge / TurnBridge.
In client mode it creates a
SessionClient and starts a listen loop.
- class network.session_manager.SessionManager(gamemaster=None, settings=None, transport_factory=None)
Bases:
objectManages the full multiplayer session lifecycle.
In host mode (DM): starts a WebSocket server,
SessionHost,EventBridge, andTurnBridge.In client mode (Player): connects a
SessionClientto a remote host.The
asyncioevent loop runs in a background daemon thread. All UI communication goes throughSessionSignals(pyqtSignal), which are thread-safe in Qt.- Parameters:
gamemaster – Gamemaster instance (required for hosting).
settings –
SettingsManagerfor defaults.transport_factory – Optional callable used for testing with
InMemoryTransportPair. When None the realWebSocketServer/WebSocketClientare used.
- claim_entity(entity_id: str)
Claim an entity (client mode only).
- Parameters:
entity_id (str) – The unique identifier of the entity to claim.
- host(port: int = 8765)
Start hosting a session on the given port.
Creates a background event loop, starts the transport server, session host, and event bridges.
- Parameters:
port (int) – TCP port to listen on.
- property is_connected: bool
- property is_hosting: bool
- join(host_addr: str, port: int, player_name: str)
Connect to a remote session as a player.
- Parameters:
host_addr (str) – The server hostname or IP address.
port (int) – The server port.
player_name (str) – Display name for this player.
- property join_address: str
- leave()
Disconnect from the remote session and shut down the background loop.
- send_chat(message: str)
Send a chat message (works in both host and client mode).
- Parameters:
message (str) – The chat text to send.
- stop_hosting()
Stop the hosted session and shut down the background event loop.
- class network.session_manager.SessionSignals
Bases:
QObjectQt signals emitted from the network thread, delivered to the UI thread.
All signals are thread-safe and can be connected to slots on the main thread from within the
SessionManagerasync callbacks.- chat_received
Emitted when a chat message arrives (sender, message).
- connected
Emitted when the client has connected successfully.
- connection_error
Emitted on connection failure (error message).
- disconnected
Emitted when the client has disconnected.
- entity_claimed
Emitted when an entity is claimed (entity_id, player_id).
- player_connected
Emitted when a player connects (player_id, player_name).
- player_disconnected
Emitted when a player disconnects (player_id).
- state_updated
Emitted when the world state has changed.
- turn_changed
Emitted when the host announces a turn change (entity name, round number).