3 Someone's Mattermost API v4 bindings.
4 Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
5 published under MIT-License
15 logger = logging.getLogger("mattermost.ws")
23 def __init__(self, ws_handler, api, ws_url):
26 self.ws_handler = ws_handler
27 self.loop = asyncio.new_event_loop()
29 self._new_websocket_thread()
32 def close_websocket(self):
34 Close the websocket and invalidate this object.
40 def _new_websocket_thread(self):
44 thread = threading.Timer(3.0, self._open_websocket)
45 thread.setName("MMws")
46 thread.setDaemon(True)
50 def _open_websocket(self):
52 self.loop.run_until_complete(self._websocket_run())
55 logger.info("websocket failed. restarting...")
56 logger.error("".join(traceback.format_exc()))
58 self._new_websocket_thread()
61 async def _websocket_run(self):
62 logger.info("Starting websocket client.")
63 async with websockets.connect(self.ws_url, ping_interval=None, extra_headers={"Authorization": "Bearer "+self.api._bearer}) as websocket:
64 #await websocket.send(json.dumps({"seq": 1, "action":"authentication_challenge", "data":{"token":self.api._bearer}}))
65 logger.info(json.loads(await websocket.recv()))
66 logger.info("websocket client connected. looping...")
69 data = json.loads(await websocket.recv())
70 if "event" not in data:
74 self.ws_handler(self, data)
76 logger.error("".join(traceback.format_exc()))