LlamaTale now supports WebSocket connections for the web browser interface, providing a modern bidirectional communication channel between the client and server. This is an alternative to the traditional Server-Sent Events (EventSource) approach.
- Bidirectional Communication: WebSocket enables real-time, two-way communication between the browser and server
- Reduced Latency: Direct WebSocket communication can be faster than HTTP polling or EventSource
- Modern Stack: Uses FastAPI and uvicorn for a modern, async Python web framework
- Backward Compatibility: The JavaScript client automatically falls back to EventSource if WebSocket is not available
Install the additional dependencies:
pip install fastapi websockets uvicornOr install all requirements including WebSocket support:
pip install -r requirements.txtTo enable WebSocket mode, use the --websocket flag when starting a game with the web interface:
python -m tale.main --game stories/dungeon --web --websocket--web: Enable web browser interface--websocket: Use WebSocket instead of EventSource (requires--web)
Standard EventSource mode (default):
python -m tale.main --game stories/dungeon --webWebSocket mode:
python -m tale.main --game stories/dungeon --web --websocketThe WebSocket implementation uses FastAPI and includes:
- TaleFastAPIApp: Main FastAPI application with WebSocket endpoint
- 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:
- Automatic Detection: Tries WebSocket first, falls back to EventSource
- 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 with WebSocket supportHttpIo: Updated to support both WSGI and FastAPI modes
-
tale/driver_if.py:IFDriver: Updated constructor withuse_websocketparameterconnect_player(): Creates FastAPI server when WebSocket mode is enabled
-
tale/web/script.js:tryWebSocket(): Attempts WebSocket connectionsetupEventSource(): Fallback to EventSourcesend_cmd(): Sends commands via WebSocket or AJAX
-
tale/main.py:- Added
--websocketcommand-line argument
- Added
- WebSocket mode is currently only supported in single-player (IF) mode
- SSL/TLS configuration may require additional setup for WebSocket secure connections
- The implementation maintains backward compatibility with the original WSGI-based approach
If the WebSocket connection fails, the client will automatically fall back to EventSource. Check:
- FastAPI and uvicorn are installed
- Port is not blocked by firewall
- Browser console for error messages
Ensure all dependencies are installed:
pip install -r requirements.txtIf FastAPI is not available, the system will fall back to the traditional WSGI server. Install FastAPI to enable WebSocket support:
pip install fastapi websockets uvicornPossible improvements for the WebSocket implementation:
- Multi-player (MUD) mode support
- Compression for large text outputs
- Reconnection handling with session persistence
- WebSocket authentication and security enhancements
- Performance metrics and monitoring