From b1d066b59fabe106ce70a822e9f6e6ba29242519 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 9 Sep 2023 23:45:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=8D=E8=BF=9E=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=E5=A4=AA=E5=A4=9A=E7=9A=84=E4=BF=9D=E9=99=A9=E6=8E=AA?= =?UTF-8?q?=E6=96=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blivedm/clients/open_live.py | 10 ++++++++++ blivedm/clients/web.py | 10 ++++++++++ blivedm/clients/ws_base.py | 4 ++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/blivedm/clients/open_live.py b/blivedm/clients/open_live.py index ad18c25..5c6169a 100644 --- a/blivedm/clients/open_live.py +++ b/blivedm/clients/open_live.py @@ -260,6 +260,16 @@ class OpenLiveClient(ws_base.WebSocketClientBase): return False return True + async def _on_before_ws_connect(self, retry_count): + """ + 在每次建立连接之前调用,可以用来初始化房间 + """ + # 重连次数太多则重新init_room,保险 + reinit_period = max(3, len(self._host_server_url_list or ())) + if retry_count > 0 and retry_count % reinit_period == 0: + self._need_init_room = True + await super()._on_before_ws_connect(retry_count) + def _get_ws_url(self, retry_count) -> str: """ 返回WebSocket连接的URL,可以在这里做故障转移和负载均衡 diff --git a/blivedm/clients/web.py b/blivedm/clients/web.py index cbe4211..e7a6d78 100644 --- a/blivedm/clients/web.py +++ b/blivedm/clients/web.py @@ -232,6 +232,16 @@ class BLiveClient(ws_base.WebSocketClientBase): return False return True + async def _on_before_ws_connect(self, retry_count): + """ + 在每次建立连接之前调用,可以用来初始化房间 + """ + # 重连次数太多则重新init_room,保险 + reinit_period = max(3, len(self._host_server_list or ())) + if retry_count > 0 and retry_count % reinit_period == 0: + self._need_init_room = True + await super()._on_before_ws_connect(retry_count) + def _get_ws_url(self, retry_count) -> str: """ 返回WebSocket连接的URL,可以在这里做故障转移和负载均衡 diff --git a/blivedm/clients/ws_base.py b/blivedm/clients/ws_base.py index ad08c23..5795afe 100644 --- a/blivedm/clients/ws_base.py +++ b/blivedm/clients/ws_base.py @@ -248,7 +248,7 @@ class WebSocketClientBase: retry_count = 0 while True: try: - await self._on_before_ws_connect() + await self._on_before_ws_connect(retry_count) # 连接 async with self._session.ws_connect( @@ -282,7 +282,7 @@ class WebSocketClientBase: logger.warning('room=%d is reconnecting, retry_count=%d', self.room_id, retry_count) await asyncio.sleep(1) - async def _on_before_ws_connect(self): + async def _on_before_ws_connect(self, retry_count): """ 在每次建立连接之前调用,可以用来初始化房间 """