From 60c60e69c5aa1a8051df732ae7cf5d67a428a683 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 18 Dec 2021 01:18:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E5=BF=83=E8=B7=B3=E5=8C=85?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blivedm/client.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/blivedm/client.py b/blivedm/client.py index 4f555b5..c8ecda4 100644 --- a/blivedm/client.py +++ b/blivedm/client.py @@ -410,9 +410,26 @@ class BLiveClient: """ 定时发送心跳包的回调 """ - coro = self._websocket.send_bytes(self._make_packet({}, Operation.HEARTBEAT)) - asyncio.ensure_future(coro, loop=self._loop) + if self._websocket is None or self._websocket.closed: + self._heartbeat_timer_handle = None + return + self._heartbeat_timer_handle = self._loop.call_later(self._heartbeat_interval, self._on_send_heartbeat) + asyncio.ensure_future(self._send_heartbeat(), loop=self._loop) + + async def _send_heartbeat(self): + """ + 发送心跳包 + """ + if self._websocket is None or self._websocket.closed: + return + + try: + await self._websocket.send_bytes(self._make_packet({}, Operation.HEARTBEAT)) + except ConnectionResetError as e: + logger.warning('room=%d _send_heartbeat() failed: %r', self.room_id, e) + except Exception: # noqa + logger.exception('room=%d _send_heartbeat() failed:', self.room_id) async def _on_ws_message(self, message: aiohttp.WSMessage): """