I have a problem with my websocket client. I would like it to always listen for new data, even if there's a short connection loss.
This is the function I use to receive data:
async def __ws_recv(self):
while self.ws.open:
try:
data = await asyncio.wait_for(self.ws.recv(), timeout=2)
except asyncio.TimeoutError:
continue
log.critical('Websocket read timeout, restarting connection...')
return
except websockets.exceptions.ConnectionClosed:
log.critical('Websocket connection is closed, restarting connection...')
return
except Exception as err_msg:
log.critical(f'Unknown error __ws_recv: {err_msg}')
os._exit(1)
self.__process_ws_data(data)
The client works fine, the only problem is that if there is a short loss of connection, the function stops on ws.recv() until the ConnectionClosed exception is raised.
For example if I turn off/on wifi (which causes 1-2 seconds of connection loss), websocket stops and needs to be restarted.
How can I keep the websocket listen for new data, without having to reconnect to the server?
I have a problem with my websocket client. I would like it to always listen for new data, even if there's a short connection loss.
This is the function I use to receive data:
The client works fine, the only problem is that if there is a short loss of connection, the function stops on ws.recv() until the ConnectionClosed exception is raised.
For example if I turn off/on wifi (which causes 1-2 seconds of connection loss), websocket stops and needs to be restarted.
How can I keep the websocket listen for new data, without having to reconnect to the server?