From 39fb86a9b20f8d94501dd2e25f16a1c573b1e33f Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 7 Jun 2019 20:02:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E9=87=8D=E8=BF=9E=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=94=BE=E5=88=B0except=E5=A4=96=E9=9D=A2(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blivedm.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/blivedm.py b/blivedm.py index 36f6298..1eb4664 100644 --- a/blivedm.py +++ b/blivedm.py @@ -373,12 +373,14 @@ class BLiveClient: if self._room_id is None: await self.init_room() + retry_count = 0 while True: heartbeat_future = None try: # 连接 async with self._session.ws_connect(WEBSOCKET_URL, ssl=self._ssl) as websocket: + retry_count = 0 self._websocket = websocket await self._send_auth() heartbeat_future = asyncio.ensure_future(self._heartbeat_loop(), loop=self._loop) @@ -395,11 +397,7 @@ class BLiveClient: break except (aiohttp.ClientConnectorError, asyncio.TimeoutError): # 重连 - logger.warning('room %d 掉线重连中', self.room_id) - # try: - # await asyncio.sleep(5) - # except asyncio.CancelledError: - # break + pass finally: if heartbeat_future is not None: heartbeat_future.cancel() @@ -409,6 +407,13 @@ class BLiveClient: break self._websocket = None + retry_count += 1 + logger.warning('room %d 掉线重连中%d', self.room_id, retry_count) + try: + await asyncio.sleep(1) + except asyncio.CancelledError: + break + self._future = None async def _heartbeat_loop(self):