测试通过
This commit is contained in:
@@ -103,6 +103,17 @@ async def _broadcast_json(clients: set[WebSocket], payload: dict) -> None:
|
||||
clients.discard(ws)
|
||||
|
||||
|
||||
async def _replace_existing_clients(clients: set[WebSocket]) -> bool:
|
||||
replaced_previous = len(clients) > 0
|
||||
if not replaced_previous:
|
||||
return False
|
||||
old_clients = list(clients)
|
||||
await asyncio.gather(*(client.close(code=1012, reason="replaced") for client in old_clients), return_exceptions=True)
|
||||
for client in old_clients:
|
||||
clients.discard(client)
|
||||
return True
|
||||
|
||||
|
||||
async def _broadcast_subtitle(
|
||||
role: str,
|
||||
text: str,
|
||||
@@ -392,6 +403,7 @@ async def events():
|
||||
@app.websocket("/ws/subtitles")
|
||||
async def ws_subtitles(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
await _replace_existing_clients(subtitle_clients)
|
||||
subtitle_clients.add(websocket)
|
||||
try:
|
||||
while True:
|
||||
@@ -406,6 +418,7 @@ async def ws_subtitles(websocket: WebSocket):
|
||||
@app.websocket("/ws/animation")
|
||||
async def ws_animation(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
await _replace_existing_clients(animation_clients)
|
||||
animation_clients.add(websocket)
|
||||
try:
|
||||
await websocket.send_text(json.dumps({"type": "animation_ready", **avatar_service.schema}, ensure_ascii=False))
|
||||
@@ -423,12 +436,7 @@ async def ws_animation(websocket: WebSocket):
|
||||
async def ws_audio(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
|
||||
replaced_previous = len(audio_clients) > 0
|
||||
if replaced_previous:
|
||||
old_clients = list(audio_clients)
|
||||
await asyncio.gather(*(client.close(code=1012, reason="replaced") for client in old_clients), return_exceptions=True)
|
||||
for client in old_clients:
|
||||
audio_clients.discard(client)
|
||||
replaced_previous = await _replace_existing_clients(audio_clients)
|
||||
|
||||
audio_clients.add(websocket)
|
||||
sample_rate = 48000
|
||||
|
||||
Reference in New Issue
Block a user