mantimon-tcg/backend/app/socketio/__init__.py
Cal Corum 0c810e5b30 Add Phase 4 WebSocket infrastructure (WS-001 through GS-001)
WebSocket Message Schemas (WS-002):
- Add Pydantic models for all client/server WebSocket messages
- Implement discriminated unions for message type parsing
- Include JoinGame, Action, Resign, Heartbeat client messages
- Include GameState, ActionResult, Error, TurnStart server messages

Connection Manager (WS-003):
- Add Redis-backed WebSocket connection tracking
- Implement user-to-sid mapping with TTL management
- Support game room association and opponent lookup
- Add heartbeat tracking for connection health

Socket.IO Authentication (WS-004):
- Add JWT-based authentication middleware
- Support token extraction from multiple formats
- Implement session setup with ConnectionManager integration
- Add require_auth helper for event handlers

Socket.IO Server Setup (WS-001):
- Configure AsyncServer with ASGI mode
- Register /game namespace with event handlers
- Integrate with FastAPI via ASGIApp wrapper
- Configure CORS from application settings

Game Service (GS-001):
- Add stateless GameService for game lifecycle orchestration
- Create engine per-operation using rules from GameState
- Implement action-based RNG seeding for deterministic replay
- Add rng_seed field to GameState for replay support

Architecture verified:
- Core module independence (no forbidden imports)
- Config from request pattern (rules in GameState)
- Dependency injection (constructor deps, method config)
- All 1090 tests passing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 22:21:20 -06:00

47 lines
1.3 KiB
Python

"""Socket.IO module for real-time game communication.
This module provides WebSocket-based real-time communication for:
- Active game sessions (actions, state updates, turn notifications)
- Connection management with session recovery
- Turn timeout handling
- JWT-based authentication
Architecture:
- Uses python-socketio with ASGI mode for FastAPI integration
- /game namespace handles all active game communication
- /lobby namespace (Phase 6) will handle matchmaking
- JWT authentication on connect (WS-004)
Usage:
The Socket.IO server is mounted alongside FastAPI in app/main.py.
Clients connect to ws://host/socket.io/ and join the /game namespace.
Client connection example:
socket = io("ws://host", {
auth: { token: "JWT_ACCESS_TOKEN" }
});
"""
from app.socketio.auth import (
AuthResult,
authenticate_connection,
cleanup_authenticated_session,
get_session_user_id,
require_auth,
setup_authenticated_session,
)
from app.socketio.server import create_socketio_app, sio
__all__ = [
# Server
"create_socketio_app",
"sio",
# Auth
"AuthResult",
"authenticate_connection",
"cleanup_authenticated_session",
"get_session_user_id",
"require_auth",
"setup_authenticated_session",
]