From 5a1456e3cb2a1ca4250f8edcacdcb52dc76f9af2 Mon Sep 17 00:00:00 2001 From: yml2213 Date: Tue, 1 Sep 2026 12:52:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=99=8E=E7=89=99=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8WSS=E5=88=9D=E5=A7=8B=E5=8C=96=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/huya/wss_client.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/core/huya/wss_client.py b/core/huya/wss_client.py index d10a882..7b91b86 100644 --- a/core/huya/wss_client.py +++ b/core/huya/wss_client.py @@ -63,6 +63,8 @@ SEQ_GETCONFIG = 0x1D000076 SEQ_REGISTER = 0x1D00003C SEQ_CONFIRM = 0x1D000002 SEQ_BUSINESS = 0x1D000106 # 业务 RPC (HAR 7.4 实证:业务帧固定使用该 seq) +SEQ_ACTIVITY_WSLAUNCH = 0x1D000100 +SEQ_ACTIVITY_BIZ = 0x1D000109 class WssCommand: @@ -113,6 +115,7 @@ class HuyaWssClient: self._heartbeat_task = None self._launch_guid = "" self._launch_ip = "" + self._activity_mode = False def _next_biz_seq(self) -> int: """业务 RPC 的 frame seq。 @@ -120,7 +123,7 @@ class HuyaWssClient: 浏览器在商城 WSS 上连续发送多个业务 RPC 时,frame seq 都保持 0x1d000106;真正区分请求依赖 WUP requestId。 """ - return SEQ_BUSINESS + return SEQ_ACTIVITY_BIZ if self._activity_mode else SEQ_BUSINESS def _next_wup_req_id(self) -> int: """业务 WUP requestId,HAR 中 getGoodsInfoV5 从 8 开始递增""" @@ -362,12 +365,16 @@ class HuyaWssClient: async def initialize_activity(self, uid: int, guid: str, cookie: str): """活动通道初始化:heartbeat -> wsLaunch -> AUTH -> getConfig -> confirm。""" + self._activity_mode = True await self.send_heartbeat() launch_rsp = await self.call_ws_launch_activity(uid, guid) if launch_rsp is None: + self.logger("[WSS] 活动 wsLaunch 无响应") return False - await self.send_auth(cookie) - await self.call_get_config_activity(uid, cookie) + await self.send_auth(cookie, sequence=SEQ_ACTIVITY_BIZ) + config_rsp = await self.call_get_config_activity(uid, cookie) + if config_rsp is None: + self.logger("[WSS] 活动 getConfig 无响应,继续发送 confirm") await self.send_confirm() await asyncio.sleep(0.1) return True @@ -392,9 +399,10 @@ class HuyaWssClient: wup.iTimeout = 0 wup.newdata["tReq"] = os.get_bytes() body = self._encode_rpc_body(wup.encode()) - msg = WssMessage(WssCommand.RPC_REQUEST, SEQ_WSLAUNCH, body) + msg = WssMessage(WssCommand.RPC_REQUEST, SEQ_ACTIVITY_WSLAUNCH, body) future = asyncio.Future() self.rpc_queue.append(future) + self.logger(format_wss_log(body, WssCommand.RPC_REQUEST, SEQ_ACTIVITY_WSLAUNCH, "发")) await self.ws.send(msg.encode()) try: response = await asyncio.wait_for(future, timeout) @@ -402,6 +410,7 @@ class HuyaWssClient: if future in self.rpc_queue: self.rpc_queue.remove(future) return None + self.logger(format_wss_log(response, WssCommand.RPC_RESPONSE, SEQ_ACTIVITY_WSLAUNCH, "收")) self._parse_launch_response(response) return response @@ -537,13 +546,13 @@ class HuyaWssClient: return body[5 : 5 + wup_len] return body - async def send_auth(self, cookie: str): + async def send_auth(self, cookie: str, sequence: int = SEQ_WSLAUNCH): """cmd 0x0a AUTH — 发送 cookie""" ua = "webh5&0.0.1&websocket&&diypc_52775" auth_text = f"huya_ua={ua}; {cookie}" msg = WssMessage( command=WssCommand.AUTH, - sequence=SEQ_WSLAUNCH, + sequence=sequence, body=auth_text.encode("utf-8") + TAIL_BYTES, ) await self.ws.send(msg.encode()) @@ -551,7 +560,7 @@ class HuyaWssClient: format_wss_log( auth_text.encode("utf-8") + TAIL_BYTES, WssCommand.AUTH, - SEQ_WSLAUNCH, + sequence, "发", ) )