feat(collector): add websocket
This commit is contained in:
33
services/collector/ws_app.py
Normal file
33
services/collector/ws_app.py
Normal file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
"""FastAPI WebSocket endpoint for live audio streaming."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||
|
||||
from ws_manager import ConnectionManager
|
||||
|
||||
app = FastAPI(title="Audio Analyzer WebSocket")
|
||||
manager = ConnectionManager()
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health() -> dict[str, str]:
|
||||
"""Health check endpoint."""
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.websocket("/ws/live")
|
||||
async def ws_live(websocket: WebSocket) -> None:
|
||||
"""WebSocket endpoint for real-time audio data streaming."""
|
||||
await manager.connect(websocket)
|
||||
try:
|
||||
while True:
|
||||
# Keep connection alive
|
||||
await websocket.receive_text()
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
await manager.disconnect(websocket)
|
||||
Reference in New Issue
Block a user