@@ -80,6 +80,7 @@ def __init__(self,
8080
8181 self .http_headers = {}
8282 self .ws = None
83+ self .ws_open = False
8384 self .waiters = {}
8485 self .identified = False
8586 self .recv_task = None
@@ -89,19 +90,20 @@ def __init__(self,
8990
9091 # Todo: remove bool return, raise error if already open
9192 async def connect (self ):
92- if self .ws != None and self .ws . open :
93+ if self .ws and self .ws_open :
9394 log .debug ('WebSocket session is already open. Returning early.' )
9495 return False
9596 self .answers = {}
9697 self .recv_task = None
9798 self .identified = False
9899 self .hello_message = None
99100 self .ws = await websockets .connect (self .url , subprotocols = ['obswebsocket.msgpack' ], additional_headers = self .http_headers , max_size = 2 ** 24 )
101+ self .ws_open = True
100102 self .recv_task = asyncio .create_task (self ._ws_recv_task ())
101103 return True
102104
103105 async def wait_until_identified (self , timeout : int = 10 ):
104- if not self .ws . open :
106+ if not self .ws_open :
105107 log .debug ('WebSocket session is not open. Returning early.' )
106108 return False
107109 try :
@@ -118,6 +120,7 @@ async def disconnect(self):
118120 self .recv_task .cancel ()
119121 await self .ws .close ()
120122 self .ws = None
123+ self .ws_open = False
121124 self .answers = {}
122125 self .identified = False
123126 self .recv_task = None
@@ -276,7 +279,7 @@ async def _send_identify(self, password, identification_parameters):
276279 await self .ws .send (msgpack .packb (identify_message ))
277280
278281 async def _ws_recv_task (self ):
279- while self .ws . open :
282+ while self .ws_open :
280283 message = ''
281284 try :
282285 message = await self .ws .recv ()
@@ -321,7 +324,9 @@ async def _ws_recv_task(self):
321324 log .warning ('Unknown OpCode: {}' .format (op_code ))
322325 except (websockets .exceptions .ConnectionClosed , websockets .exceptions .ConnectionClosedError , websockets .exceptions .ConnectionClosedOK ):
323326 log .debug ('The WebSocket connection was closed. Code: {} | Reason: {}' .format (self .ws .close_code , self .ws .close_reason ))
327+ self .ws_open = False
324328 break
325329 except (ValueError , msgpack .UnpackException ):
326330 continue
331+ self .ws_open = False
327332 self .identified = False
0 commit comments