@@ -192,15 +192,20 @@ def running(self):
192192 """Whether the connection loop is running."""
193193 return self ._loop_task .task is not None and not self ._loop_task .done ()
194194
195- async def _close_ws (self ):
196- """Close WebSocket connection manually, typically used when we are paused or stopped and no reconnections are taking place."""
197- if self .connected :
198- await self .ws .close ()
199- self .ws = None
200- self .logger .debug ("Emitting ConnectionLostEvent due to manual close." )
201- _ = self .event_bus .emit_task (ConnectionLostEvent (self .v ))
202- self .v += 1
203- self .logger .info ("Manually closed connection." )
195+ async def _close_ws (self , code : int = WSCloseCode .OK , message : bytes = b"" ):
196+ """
197+ Close WebSocket connection manually, typically used when we are paused or
198+ stopped and no reconnections are taking place.
199+ """
200+ if not self .connected :
201+ return
202+ await self .ws .close (code = code , message = message )
203+ self .ws = None
204+ self .logger .debug ("Emitting ConnectionLostEvent due to manual close." )
205+ self ._state = State .NOT_CONNECTED
206+ _ = self .event_bus .emit_task (ConnectionLostEvent (self .v ))
207+ self .v += 1
208+ self .logger .info ("Manually closed connection." )
204209
205210 async def _loop (self ):
206211 """Connection main loop - only run once per instance."""
@@ -355,16 +360,11 @@ async def _loop(self):
355360
356361 # Reset connection if waiter is done without having received first message.
357362 if not has_first_msg and wait_first_msg_task .done ():
358- await self .ws . close (
363+ await self ._close_ws (
359364 code = WSCloseCode .PROTOCOL_ERROR ,
360365 message = b"Did not receive first message in time." ,
361366 )
362367
363- raise ConnectionResetError (
364- f"Did not receive first message in less"
365- f" than { WsFirstMessageTimeout } seconds."
366- )
367-
368368 except WsConnectionErrors as e :
369369 # Disconnect / No connection handling
370370 self ._state = State .NOT_CONNECTED
0 commit comments