@@ -61,36 +61,34 @@ async def send(self, message: Message) -> None:
61
61
"""
62
62
Send ASGI websocket messages, ensuring valid state transitions.
63
63
"""
64
- try :
65
- if self .application_state == WebSocketState .CONNECTING :
66
- message_type = message ["type" ]
67
- if message_type not in {"websocket.accept" , "websocket.close" }:
68
- raise RuntimeError (
69
- 'Expected ASGI message "websocket.accept" or '
70
- f'"websocket.close", but got { message_type !r} '
71
- )
72
- if message_type == "websocket.close" :
73
- self .application_state = WebSocketState .DISCONNECTED
74
- else :
75
- self .application_state = WebSocketState .CONNECTED
76
- await self ._send (message )
77
- elif self .application_state == WebSocketState .CONNECTED :
78
- message_type = message ["type" ]
79
- if message_type not in {"websocket.send" , "websocket.close" }:
80
- raise RuntimeError (
81
- 'Expected ASGI message "websocket.send" or "websocket.close", '
82
- f"but got { message_type !r} "
83
- )
84
- if message_type == "websocket.close" :
85
- self .application_state = WebSocketState .DISCONNECTED
86
- await self ._send (message )
64
+ if self .application_state == WebSocketState .CONNECTING :
65
+ message_type = message ["type" ]
66
+ if message_type not in {"websocket.accept" , "websocket.close" }:
67
+ raise RuntimeError (
68
+ 'Expected ASGI message "websocket.accept" or '
69
+ f'"websocket.close", but got { message_type !r} '
70
+ )
71
+ if message_type == "websocket.close" :
72
+ self .application_state = WebSocketState .DISCONNECTED
87
73
else :
74
+ self .application_state = WebSocketState .CONNECTED
75
+ await self ._send (message )
76
+ elif self .application_state == WebSocketState .CONNECTED :
77
+ message_type = message ["type" ]
78
+ if message_type not in {"websocket.send" , "websocket.close" }:
88
79
raise RuntimeError (
89
- 'Cannot call "send" once a close message has been sent.'
80
+ 'Expected ASGI message "websocket.send" or "websocket.close", '
81
+ f"but got { message_type !r} "
90
82
)
91
- except IOError : # pragma: no cover
92
- self .application_state = WebSocketState .DISCONNECTED
93
- raise WebSocketDisconnect (code = 1006 )
83
+ if message_type == "websocket.close" :
84
+ self .application_state = WebSocketState .DISCONNECTED
85
+ try :
86
+ await self ._send (message )
87
+ except IOError :
88
+ self .application_state = WebSocketState .DISCONNECTED
89
+ raise WebSocketDisconnect (code = 1006 )
90
+ else :
91
+ raise RuntimeError ('Cannot call "send" once a close message has been sent.' )
94
92
95
93
async def accept (
96
94
self ,
0 commit comments