-
-
Notifications
You must be signed in to change notification settings - Fork 7
Add WebSocket support to browser IO using FastAPI #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Added FastAPI, websockets, and uvicorn to requirements.txt - Created TaleFastAPIApp class with WebSocket endpoint in if_browser_io.py - Updated HttpIo class to support both WSGI and FastAPI modes - Modified driver_if.py to support WebSocket mode via use_websocket flag - Updated script.js to support WebSocket connections with EventSource fallback - WebSocket sends/receives JSON messages for bidirectional communication Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
- Added --websocket flag to main.py for enabling WebSocket mode - Updated aiohttp requirement to >=3.9.0 for Python 3.12 compatibility - WebSocket mode can now be enabled with: python -m tale.main --game <path> --web --websocket Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
- Extract error message display logic into reusable function in script.js - Remove unused Empty exception import from if_browser_io.py - Fix WebSocket exception handling to remove unreachable code - Fix JavaScript syntax error in protocol detection - Add documentation for WebSocket feature (WEBSOCKET.md) Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
- Fix race condition in WebSocket connection detection using connectionEstablished flag - Add adaptive timeout in WebSocket loop: short timeout (0.1s) when active, long timeout (0.5s) when idle - Reduce CPU usage by sleeping longer (0.1s) when no activity detected - Improve error handling for initial connection failures vs established connection errors Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
- Add error handling for WebSocket send failures with fallback to AJAX - Extract sendViaAjax helper function to reduce code duplication - Improve WebSocket exception handling with proper logging and traceback - Add specific handling for asyncio.CancelledError - Extract _process_command method to eliminate duplicate command processing logic - Add player context to error messages for better debugging Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
- Created IMPLEMENTATION_SUMMARY.md with complete implementation details - Documented all components, message protocol, and quality assurance - Added testing recommendations and future enhancement ideas - Includes compatibility notes and usage examples Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
neph1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good start. Using it, however, yields the following error:
File "/LlamaTale/tale/tio/if_browser_io.py", line 131, in HttpIo.singleplayer_mainloop
Source code:
hostname = player_connection.driver.story.config.mud_host
Local values:
Thread = <class 'threading.Thread'>
player_connection = <tale.player.PlayerConnection object at 0x7f19b6f3f730>
protocol = 'http'
self = <tale.tio.if_browser_io.HttpIo object at 0x7f19b61fbc10>
webbrowser = <module 'webbrowser' from '/usr/lib/python3.10/webbrowser.py'>
EXCEPTION HERE: AttributeError: 'PlayerConnection' object has no attribute 'driver'
@copilot please investigate this
…layer_connection The player_connection object doesn't have a driver attribute. Changed to access story config through self.fastapi_server.driver.story.config instead of player_connection.driver.story.config in singleplayer_mainloop method. Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
Fixed in commit 365cf63. The issue was that |
|
@copilot that fixed the issue and the server now runs. do you know why? |
The To use the WebSocket:
The WebSocket connection happens automatically from the story page via JavaScript. If you want to verify the WebSocket is working, open the browser console and look for messages like "WebSocket connection established" or "Falling back to EventSource". |
|
@copilot a couple of tests are failing in test_browser, due to expecting a wsgi server. can you fix these, or remove them if it's being tested elsewhere? |
…er to server The HttpIo.__init__() signature was changed to use 'server' instead of 'wsgi_server' as part of the WebSocket implementation. Updated the tests to use the new parameter name. Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
Fixed in commit eefb92c. The tests were using the old parameter name |
Add WebSocket support to IO using FastAPI
Summary
Successfully implemented WebSocket support for LlamaTale's web browser interface using FastAPI, as requested in the issue. The implementation provides a modern, bidirectional communication channel while maintaining full backward compatibility.
Features Implemented
✅ WebSocket Endpoint: FastAPI-based WebSocket at
/tale/ws✅ Core Methods:
get_player_from_headers(),handle_player_input(),cleanup_player()✅ Client Fallback: Automatic detection with EventSource fallback
✅ Performance: Adaptive timeouts (0.1s active, 0.5s idle) for low CPU usage
✅ Error Handling: Comprehensive logging, graceful degradation, player context
✅ Security: Zero vulnerabilities (CodeQL verified)
✅ Documentation: Complete user guide (WEBSOCKET.md) and implementation summary
Usage
Quality Assurance
Files Changed
requirements.txt- Added FastAPI dependenciestale/tio/if_browser_io.py- TaleFastAPIApp classtale/driver_if.py- WebSocket mode initializationtale/main.py- Added--websocketflagtale/web/script.js- WebSocket client with fallbackWEBSOCKET.md- User documentationIMPLEMENTATION_SUMMARY.md- Technical documentationtests/test_browser.py- Fixed tests to use new HttpIo init parameterOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.