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): """ 在每次建立连接之前调用,可以用来初始化房间 """