Spaces:
Building
Building
Update websocket_handler.py
Browse files- websocket_handler.py +37 -22
websocket_handler.py
CHANGED
@@ -392,6 +392,11 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
|
392 |
try:
|
393 |
while True:
|
394 |
try:
|
|
|
|
|
|
|
|
|
|
|
395 |
# Receive message with timeout
|
396 |
message = await asyncio.wait_for(
|
397 |
websocket.receive_json(),
|
@@ -409,35 +414,45 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
|
409 |
|
410 |
elif message_type == "ping":
|
411 |
# Keep-alive ping - log yapmadan
|
412 |
-
|
|
|
413 |
|
414 |
except asyncio.TimeoutError:
|
415 |
# Timeout log'unu da azaltalım - her timeout'ta değil
|
416 |
-
|
417 |
-
|
|
|
418 |
except WebSocketDisconnect as e:
|
419 |
log_info(f"🔌 WebSocket disconnected", session_id=session_id, code=e.code, reason=e.reason)
|
420 |
except Exception as e:
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
log_info(f"🧹 Cleaning up WebSocket connection", session_id=session_id)
|
433 |
-
await realtime_session.cleanup()
|
434 |
-
|
435 |
-
# WebSocket'in açık olup olmadığını kontrol et
|
436 |
try:
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
441 |
|
442 |
# ========================= MESSAGE HANDLERS =========================
|
443 |
async def handle_audio_chunk(websocket: WebSocket, session: RealtimeSession, message: Dict[str, Any]):
|
|
|
392 |
try:
|
393 |
while True:
|
394 |
try:
|
395 |
+
# WebSocket aktif mi kontrol et
|
396 |
+
if not realtime_session.is_websocket_active:
|
397 |
+
log_info(f"🔌 WebSocket inactive, breaking loop", session_id=session_id)
|
398 |
+
break
|
399 |
+
|
400 |
# Receive message with timeout
|
401 |
message = await asyncio.wait_for(
|
402 |
websocket.receive_json(),
|
|
|
414 |
|
415 |
elif message_type == "ping":
|
416 |
# Keep-alive ping - log yapmadan
|
417 |
+
if realtime_session.is_websocket_active:
|
418 |
+
await websocket.send_json({"type": "pong"})
|
419 |
|
420 |
except asyncio.TimeoutError:
|
421 |
# Timeout log'unu da azaltalım - her timeout'ta değil
|
422 |
+
if realtime_session.is_websocket_active:
|
423 |
+
await websocket.send_json({"type": "ping"})
|
424 |
+
|
425 |
except WebSocketDisconnect as e:
|
426 |
log_info(f"🔌 WebSocket disconnected", session_id=session_id, code=e.code, reason=e.reason)
|
427 |
except Exception as e:
|
428 |
+
# WebSocket kapalıysa hata verme
|
429 |
+
if "WebSocket is not connected" not in str(e) and "Cannot call \"send\"" not in str(e):
|
430 |
+
log_error(
|
431 |
+
f"❌ WebSocket error",
|
432 |
+
error=str(e),
|
433 |
+
traceback=traceback.format_exc(),
|
434 |
+
session_id=session_id
|
435 |
+
)
|
436 |
+
|
437 |
+
# Error mesajı göndermeye çalışma, zaten kapalı olabilir
|
438 |
+
if realtime_session.is_websocket_active:
|
|
|
|
|
|
|
|
|
439 |
try:
|
440 |
+
await websocket.send_json({
|
441 |
+
"type": "error",
|
442 |
+
"message": str(e)
|
443 |
+
})
|
444 |
+
except:
|
445 |
+
pass
|
446 |
+
finally:
|
447 |
+
log_info(f"🧹 Cleaning up WebSocket connection", session_id=session_id)
|
448 |
+
await realtime_session.cleanup()
|
449 |
+
|
450 |
+
# WebSocket'in açık olup olmadığını kontrol et
|
451 |
+
try:
|
452 |
+
if websocket.client_state.value == 1: # 1 = CONNECTED state
|
453 |
+
await websocket.close()
|
454 |
+
except Exception as e:
|
455 |
+
log_debug(f"WebSocket already closed or error during close: {e}", session_id=session_id)
|
456 |
|
457 |
# ========================= MESSAGE HANDLERS =========================
|
458 |
async def handle_audio_chunk(websocket: WebSocket, session: RealtimeSession, message: Dict[str, Any]):
|