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()