feat(02-02): wire WebSocket broadcast loop and command dispatch

- Add broadcast_loop sending position ticks at 10Hz to all clients
- Replace echo stub with play/pause/seek/load command dispatch to MPVEngine
- Start broadcast_loop as asyncio task in lifespan, cancel on shutdown
- Use get_engine callable to avoid circular import at module level
This commit is contained in:
Claude
2026-04-06 13:07:18 +00:00
parent 3173fa2228
commit 5e400a6834
2 changed files with 51 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
import asyncio
import os
from contextlib import asynccontextmanager
from pathlib import Path
@@ -27,8 +28,15 @@ async def lifespan(app: FastAPI):
engine = MPVEngine(ao=ao)
engine.start()
app.state.engine = engine
from lightsync.api.ws import broadcast_loop
task = asyncio.create_task(broadcast_loop(lambda: engine))
yield
# Shutdown
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
engine.stop()
await registry.save()