test voice rooms
This commit is contained in:
39
app/api/voice_ws.py
Normal file
39
app/api/voice_ws.py
Normal file
@ -0,0 +1,39 @@
|
||||
from fastapi import APIRouter, WebSocket, WebSocketDisconnect, Query
|
||||
from app.realtime.voice_hub import voice_hub
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.websocket("/ws/voice")
|
||||
async def voice_ws(
|
||||
ws: WebSocket,
|
||||
room_id: str = Query(...),
|
||||
username: str = Query(...)
|
||||
):
|
||||
await voice_hub.connect(room_id, username, ws)
|
||||
|
||||
# уведомим остальных
|
||||
await voice_hub.broadcast(
|
||||
room_id,
|
||||
{"type": "join", "user": username}
|
||||
)
|
||||
|
||||
try:
|
||||
while True:
|
||||
msg = await ws.receive_json()
|
||||
|
||||
if msg["type"] == "signal":
|
||||
await voice_hub.send_to(
|
||||
room_id,
|
||||
msg["to"],
|
||||
{
|
||||
"type": "signal",
|
||||
"from": username,
|
||||
"data": msg["data"]
|
||||
}
|
||||
)
|
||||
except WebSocketDisconnect:
|
||||
voice_hub.disconnect(room_id, username)
|
||||
await voice_hub.broadcast(
|
||||
room_id,
|
||||
{"type": "leave", "user": username}
|
||||
)
|
||||
Reference in New Issue
Block a user