LlamaTale uses WebSocket connections for the web browser interface in both single-player (IF) mode and multi-player (MUD) mode, providing a modern bidirectional communication channel between the client and server.
- Bidirectional Communication: WebSocket enables real-time, two-way communication between the browser and server
- Reduced Latency: Direct WebSocket communication is faster than HTTP polling or EventSource
- Modern Stack: Uses FastAPI and uvicorn for a modern, async Python web framework
- Unified Approach: Both IF and MUD modes now use the same WebSocket-based architecture
Install the required dependencies:
pip install fastapi websockets uvicornOr install all requirements:
pip install -r requirements.txtpython -m tale.main --game stories/dungeon --webpython -m tale.main --game stories/dungeon --mode mudThe WebSocket implementation uses FastAPI and includes:
- TaleFastAPIApp (IF mode): FastAPI application for single-player with WebSocket endpoint
- TaleMudFastAPIApp (MUD mode): FastAPI application for multi-player with session management
- WebSocket Endpoint (
/tale/ws): Handles bidirectional communication - HTTP Routes: Serves static files and HTML pages
- Message Protocol: JSON-based messages for commands and responses
The JavaScript client (script.js) includes:
- WebSocket Connection: Connects to the WebSocket endpoint
- Message Handling: Processes incoming text, data, and status messages
- Command Sending: Sends commands and autocomplete requests via WebSocket
Client to Server:
{
"cmd": "look around",
"autocomplete": 0
}Server to Client (text):
{
"type": "text",
"text": "<p>You see a dark corridor...</p>",
"special": [],
"turns": 42,
"location": "Dark Corridor",
"location_image": "",
"npcs": "goblin,troll",
"items": "sword,potion",
"exits": "north,south"
}Server to Client (data):
{
"type": "data",
"data": "base64_encoded_image..."
}-
tale/tio/if_browser_io.py:TaleFastAPIApp: FastAPI application for single-player modeHttpIo: I/O adapter for the FastAPI web server
-
tale/tio/mud_browser_io.py:TaleMudFastAPIApp: FastAPI application for multi-player mode with session managementMudHttpIo: I/O adapter for multi-player browser interface
-
tale/driver_if.py:IFDriver: Creates FastAPI server for IF web interface
-
tale/driver_mud.py:MudDriver: Creates FastAPI server for MUD web interface
-
tale/web/script.js:connectWebSocket(): Establishes WebSocket connectionsend_cmd(): Sends commands via WebSocket
If the WebSocket connection fails:
- Ensure FastAPI and uvicorn are installed
- Check that the port is not blocked by firewall
- Check browser console for error messages
Ensure all dependencies are installed:
pip install fastapi websockets uvicornIf FastAPI is not available, an error will be raised when starting with web interface. Install FastAPI:
pip install fastapi websockets uvicornPossible improvements for the WebSocket implementation:
- Compression for large text outputs
- Reconnection handling with session persistence
- WebSocket authentication and security enhancements
- Performance metrics and monitoring