Add WebSocket support to browser IO using FastAPI #126
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "copilot/add-web-socket-support"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
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
Fixed in commit
365cf63. The issue was thatplayer_connectiondoesn't have adriverattribute. Changed to access the story config throughself.fastapi_server.driver.story.configinstead.@copilot that fixed the issue and the server now runs.
i start with the --websocket flag, but when accessing the endpoint http://localhost:8180/tale/ws in the browser, i receive this:
{
"detail": "Not Found"
}
do you know why?
The
/tale/wsendpoint is a WebSocket endpoint, not a regular HTTP endpoint. You can't access it directly via browser URL - that's why you get "Not Found".To use the WebSocket:
http://localhost:8180/tale/story(the game page)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?
Fixed in commit
eefb92c. The tests were using the old parameter namewsgi_serverbut theHttpIo.__init__()signature was changed to useserveras part of the WebSocket implementation. Updated the 4 failing tests to use the new parameter name. All 6 tests now pass.