fix voice rooms
This commit is contained in:
@ -9,18 +9,27 @@ async def voice_ws(
|
|||||||
room_id: str = Query(...),
|
room_id: str = Query(...),
|
||||||
username: str = Query(...)
|
username: str = Query(...)
|
||||||
):
|
):
|
||||||
|
if username in voice_hub.rooms.get(room_id, {}):
|
||||||
|
await ws.close(code=4001)
|
||||||
|
return
|
||||||
|
|
||||||
await voice_hub.connect(room_id, username, ws)
|
await voice_hub.connect(room_id, username, ws)
|
||||||
|
|
||||||
# уведомим остальных
|
users = list(voice_hub.rooms.get(room_id, {}).keys())
|
||||||
await voice_hub.broadcast(
|
await ws.send_json({
|
||||||
|
"type": "users",
|
||||||
|
"users": users
|
||||||
|
})
|
||||||
|
|
||||||
|
await voice_hub.broadcast_except(
|
||||||
room_id,
|
room_id,
|
||||||
|
username,
|
||||||
{"type": "join", "user": username}
|
{"type": "join", "user": username}
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
msg = await ws.receive_json()
|
msg = await ws.receive_json()
|
||||||
|
|
||||||
if msg["type"] == "signal":
|
if msg["type"] == "signal":
|
||||||
await voice_hub.send_to(
|
await voice_hub.send_to(
|
||||||
room_id,
|
room_id,
|
||||||
|
|||||||
@ -25,6 +25,17 @@ class VoiceHub:
|
|||||||
|
|
||||||
async def broadcast(self, room_id: str, payload: dict):
|
async def broadcast(self, room_id: str, payload: dict):
|
||||||
for ws in self.rooms.get(room_id, {}).values():
|
for ws in self.rooms.get(room_id, {}).values():
|
||||||
await ws.send_json(payload)
|
try:
|
||||||
|
await ws.send_json(payload)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def broadcast_except(self, room_id: str, except_user: str, payload: dict):
|
||||||
|
for user, ws in self.rooms.get(room_id, {}).items():
|
||||||
|
if user != except_user:
|
||||||
|
try:
|
||||||
|
await ws.send_json(payload)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
voice_hub = VoiceHub()
|
voice_hub = VoiceHub()
|
||||||
|
|||||||
Reference in New Issue
Block a user