You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromfastapiimportFastAPIfromfastapi.websocketsimportWebSocket, WebSoketState, WebSocketDisapp=FastAPI()
@app.websocket('/foo')asyncdefbar(
websocket: WebSocket
):
awaitwebsocket.accepted()
try:
service_functinos(websocket) # WebSocketDisconnect raised by client, while doing this one.exceptWebSocketDisconnect:
passfinally:
# True, because there is no code about chaing state in `_raise_on_disconnect`still_connected=websocket.application_state==WebSocketState.CONNECTEDifstill_connected:
awaitwebsocket.close() # make second error, about closing twice
May be, you can think about there is no problem replace return to pass and add close function to try scope.
But In that case we can't close it when unexpected error occured : (
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello!
How about adding
self.application_state = WebSocketState.DISCONNECTED
to under the if statement?In my opinion, it can make more specify to handling closing websocket.
starlette/starlette/websockets.py
Lines 111 to 114 in c2e3a39
This change will be suitable for below example.
May be, you can think about there is no problem replace
return
to pass and add close function to try scope.But In that case we can't close it when unexpected error occured : (
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions