You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 22, 2026. It is now read-only.
While i was using the library the websocket connection would not be recreated if network was down. So i have come to own version of Websocket class:
importtypingimportasyncioimportjsonimportloggingimportaiohttplog=logging.getLogger("mattermostdriver.websocket")
classWebsocket:
heartbeat: int=5receive_timeout: int=10keepalive_delay: float=5def__init__(self, options: typing.Dict[str, typing.Any], token: str):
self.options=optionsself._token=tokenself._alive=Falseasyncdefconnect(
self,
event_handler: typing.Callable[[str], typing.Awaitable[None]],
) ->None:
url="wss://{url:s}:{port:s}{basepath:s}/websocket".format(
url=self.options["url"],
port=str(self.options["port"]),
basepath=self.options["basepath"],
)
self._alive=Truewhileself._alive:
try:
asyncwithaiohttp.ClientSession() assession:
# The receive_timeout parameter allows you not to block the cycle of receiving messages and throws an error# TimeoutError(by the way, if you do not do _authenticate, then there will be no error, the loop will just end)# after receive_timeout seconds if no messages have been received.# At the same time, the heartbeat parameter ensures that every heartbeat of seconds should come# at least a PONG message, and if it does not come, it means that the connection is broken and you need to# recreate the connectionasyncwithsession.ws_connect(
url,
heartbeat=self.heartbeat,
receive_timeout=self.receive_timeout,
verify_ssl=self.options["verify"],
) aswebsocket:
awaitself._authenticate(websocket)
asyncformessageinwebsocket:
awaitevent_handler(message.data)
exceptExceptionase:
log.exception(
f"Failed to establish websocket connection: {type(e)} thrown",
)
awaitasyncio.sleep(self.keepalive_delay)
defdisconnect(self) ->None:
log.info("Disconnecting websocket")
self._alive=Falseasyncdef_authenticate(self, websocket: aiohttp.client.ClientWebSocketResponse) ->None:
log.info("Authenticating websocket")
json_data=json.dumps(
{
"seq": 1,
"action": "authentication_challenge",
"data": {"token": self._token},
},
)
awaitwebsocket.send_str(json_data)
It looks simplier and reconnect works perfectly. I can make a pull request if needs
While i was using the library the websocket connection would not be recreated if network was down. So i have come to own version of Websocket class:
It looks simplier and reconnect works perfectly. I can make a pull request if needs