From 0bdec42f5007cb1373eb39fafa8dfdd9070bcacf Mon Sep 17 00:00:00 2001 From: kinori Date: Sat, 2 Sep 2023 19:36:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=BC=80=E6=94=BE=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E6=8E=A5=E5=8F=A3=E7=9A=84=E7=BB=93=E6=9D=9F=E5=8F=8A?= =?UTF-8?q?=E5=BF=83=E8=B7=B3=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blivedm/client.py | 11 +++++++++++ blivedm/open_live_client.py | 5 ++--- open_live_sample.py | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/blivedm/client.py b/blivedm/client.py index e05d5c9..174b635 100644 --- a/blivedm/client.py +++ b/blivedm/client.py @@ -233,6 +233,10 @@ class BLiveClient: """ 便利函数,停止本客户端并释放本客户端的资源,调用后本客户端将不可用 """ + + if self._open_live_client: + await self._open_live_client.end() + if self.is_running: self.stop() await self.join() @@ -522,6 +526,13 @@ class BLiveClient: except Exception: # noqa logger.exception('room=%d _send_heartbeat() failed:', self.room_id) + try: + await self._open_live_client.heartbeat() + except (ConnectionResetError, aiohttp.ClientConnectionError) 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): """ 收到WebSocket消息 diff --git a/blivedm/open_live_client.py b/blivedm/open_live_client.py index e010bbe..f938f35 100644 --- a/blivedm/open_live_client.py +++ b/blivedm/open_live_client.py @@ -149,7 +149,7 @@ class OpenLiveClient: return False try: - params = f'{{"app_id":{self.app_id},"game_id":{self._game_id}}}' + params = f'{{"game_id":"{self._game_id}", "app_id":{self.app_id}}}' headers = self._sign_request_header(params) async with self._session.post( OPEN_LIVE_END_URL, headers=headers, data=params, ssl=self._ssl @@ -175,8 +175,7 @@ class OpenLiveClient: return False try: - params = f'{{"game_id":{self._game_id}}}' - print(params) + params = f'{{"game_id":"{self._game_id}"}}' headers = self._sign_request_header(params) async with self._session.post( OPEN_LIVE_HEARTBEAT_URL, headers=headers, data=params, ssl=self._ssl diff --git a/open_live_sample.py b/open_live_sample.py index bf21222..f705b20 100644 --- a/open_live_sample.py +++ b/open_live_sample.py @@ -25,8 +25,8 @@ async def run_start(): client.start() try: - # 演示60秒后停止 - await asyncio.sleep(600) + # 演示20秒后停止 + await asyncio.sleep(60) client.stop() await client.join()