handle websocket disconnect
This commit is contained in:
15
app.py
15
app.py
@@ -12,6 +12,7 @@ from fastapi import (
|
|||||||
FastAPI,
|
FastAPI,
|
||||||
Request,
|
Request,
|
||||||
WebSocket,
|
WebSocket,
|
||||||
|
WebSocketDisconnect,
|
||||||
Depends,
|
Depends,
|
||||||
HTTPException,
|
HTTPException,
|
||||||
status,
|
status,
|
||||||
@@ -95,10 +96,14 @@ async def websocket_endpoint(websocket: WebSocket):
|
|||||||
websocket: The WebSocket connection.
|
websocket: The WebSocket connection.
|
||||||
"""
|
"""
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
while True:
|
try:
|
||||||
data = await websocket.receive_text()
|
while True:
|
||||||
|
data = await websocket.receive_text()
|
||||||
|
|
||||||
async for chunk in generate(data):
|
async for chunk in generate(data):
|
||||||
await websocket.send_text(chunk)
|
await websocket.send_text(chunk)
|
||||||
|
|
||||||
await websocket.send_text("<<END>>")
|
await websocket.send_text("<<END>>")
|
||||||
|
except WebSocketDisconnect:
|
||||||
|
logger.info("Client disconnected")
|
||||||
|
await websocket.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user