Skip to content

Commit

Permalink
Always try reconnect to websocket if connection is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
sebr committed Aug 30, 2020
1 parent adc0b89 commit ab4e681
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions custom_components/bhyve/pybhyve/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ async def running(self):

elif msg.type == WSMsgType.CLOSE:
_LOGGER.debug("Websocket received CLOSE message, ignoring")
# await self._ws.close()
# break
break

elif msg.type == WSMsgType.CLOSED:
_LOGGER.error("websocket connection closed")
Expand All @@ -126,27 +125,26 @@ async def running(self):

if self._ws.closed:
_LOGGER.info("Websocket closed? %s", self._ws.closed)
self.state = STATE_STOPPED

if self._ws.exception():
_LOGGER.warning(
"Websocket exception: %s", self._ws.exception() or "Unknown"
)
self.state = STATE_STOPPED

except aiohttp.ClientConnectorError:
_LOGGER.error("Client connection error; state: %s", self.state)
if self.state != STATE_STOPPED:
self.retry()
self.retry()

# pylint: disable=broad-except
except Exception as err:
_LOGGER.error("Unexpected error %s", err)
if self.state != STATE_STOPPED:
self.retry()
self.retry()

else:
if self.state != STATE_STOPPED:
_LOGGER.info("Reconnecting websocket; state: %s", self.state)
self.retry()
_LOGGER.info("Reconnecting websocket; state: %s", self.state)
self.retry()

async def stop(self):
"""Close websocket connection."""
Expand All @@ -170,4 +168,6 @@ async def send(self, payload):
if not self._ws.closed:
await self._ws.send_str(json.dumps(payload))
else:
_LOGGER.warning("Tried to send message whilst websocket closed; state: %s", self.state)
_LOGGER.warning(
"Tried to send message whilst websocket closed; state: %s", self.state
)

0 comments on commit ab4e681

Please sign in to comment.