handle closing ws event

This commit is contained in:
Maxim Devaev 2022-07-19 16:25:31 +03:00
parent 54cb5e1fed
commit b16359c53e
2 changed files with 7 additions and 1 deletions

View File

@ -166,7 +166,7 @@ class _Client(RfbClient): # pylint: disable=too-many-instance-attributes
self.__stage3_ws_connected.set_passed()
async for event in self.__kvmd_ws.communicate():
await self.__process_ws_event(event)
raise RfbError("KVMD closes the websocket (the server may have been stopped)")
raise RfbError("KVMD closed the websocket (the server may have been stopped)")
finally:
self.__kvmd_ws = None

View File

@ -153,6 +153,8 @@ class KvmdClientWs:
msg = receive_task.result()
if msg.type == aiohttp.WSMsgType.TEXT:
yield json.loads(msg.data)
elif msg.type == aiohttp.WSMsgType.CLOSE:
await self.__ws.close()
elif msg.type == aiohttp.WSMsgType.CLOSED:
break
else:
@ -167,6 +169,10 @@ class KvmdClientWs:
receive_task.cancel()
if writer_task:
writer_task.cancel()
try:
await self.__ws.close()
except Exception:
pass
self.__communicated = False
async def send_key_event(self, key: str, state: bool) -> None: