方法名称 | 方法解释 |
---|---|
on_open | 发送参数给服务器调用该方法 |
on_message | 接受服务器返回信息调用该方法 |
on_error | 程序报错调用该方法 |
on_close | 程序断开调用该方法 |
●WebSocketApp代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#@Users: LiMu
#@Files:WebSocketApps.py
#@Times: 2021/12/29
#@Software:PyCharm
import os
import sys
import time
import json
import zlib
import websocket
from jsonpath import jsonpath
#pip install websocket-client
class WebSocketApps(object):
def __init__(self,Url:str,premises:list):
self.premise = premises
#当报错module 'websocket' has no attribute 'enableTrace'时使用不报错可以注释
websocket.enableTrace(True)
#向服务器建立连接
self.ws = websocket.WebSocketApp(url=Url,on_message = self.on_message,on_error = self.on_error,on_close = self.on_close)
self.ws.on_open = self.on_open
#发送心跳时间间隔ping_interval,心跳超时时间ping_timeout
self.ws.run_forever(ping_interval=3, ping_timeout=1)
#接受消息调用方法on_message()
def on_message(self,ws,message):
#对服务器返回数据进行处理
message = zlib.decompress(message)
message = json.loads(message)
#验证登录是否成功,并获取登录返回Session组装业务参数发送给服务器
if message.get("HandleCode") == "A001" and message.get("StatusMsg")=="成功":
print("{} 返回信息:{}\n{}".format(message["HandleCode"], message,"="*180))
for i in self.premise:
i["PlayerId"] = jsonpath(message, "$.Value.PlayerId")[0]
i["Session"] = jsonpath(message, "$.Value.Session")[0]
#发送组装完成后的业务参数
ws.send(json.dumps(i))
print("{} 参数发送:{}".format(i["HandleCode"], i))
elif "A" in message["HandleCode"] and message.get("StatusMsg")=="成功":
print("{} 返回信息:{}".format(message["HandleCode"], message))
else:
# print(f"A00 推送信息:{message}")
pass
#程序调错时调用方法on_error()
def on_error(self,ws,error):
print("{}返回错误:{}".format(ws,error))
#主动断开时调用方法on_close()
def on_close(self,ws):
self.ws.close()
print("{}关闭链接{}".format(ws,"closed"))
#建立连接时调用方法on_open()
def on_open(self,ws):
LoginInfo = {'HandleCode': 'A001', 'ModuleName': 'Player', 'MethodName': 'Login_ForDebugTest','PartnerId': '1001', 'ServerId': '21439', 'PlayerId': '', 'Session': '','GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['AmmOne']}
ws.send(json.dumps(LoginInfo))
print("{} 参数发送:{}".format(LoginInfo["HandleCode"], LoginInfo))
if __name__ == '__main__':
#业务参数
GetHero1 = {'HandleCode': 'A002', 'ModuleName': 'Hero', 'MethodName': 'GetInfo', 'PartnerId': '1001', 'ServerId': '21439', 'PlayerId': '', 'Session': '', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['12011110']}
GetHero2 = {'HandleCode': 'A003', 'ModuleName': 'HeroMastery', 'MethodName': 'GetInfo', 'PartnerId': '1001','ServerId': '21439', 'PlayerId': '', 'Session': '', 'GameVersionId': '1212121','ResourceVersionName': '2232a', 'Data': []}
#启动服务并传入参数
Responses = WebSocketApps("ws://10.1.0.85:21440/client",[GetHero1,GetHero2])
● 运行结果如下所示:
D:\Python\python.exe E:\pythonFiles\Interfacetest\WebSocketApps.py
A001 参数发送:{'HandleCode': 'A001', 'ModuleName': 'Player', 'MethodName': 'Login_ForDebugTest', 'PartnerId': '1001', 'ServerId': '21439', 'PlayerId': '', 'Session': '', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['AmmOne']}
--- request header ---
GET /client HTTP/1.1
Upgrade: websocket
Host: 10.1.0.85:21440
Origin: http://10.1.0.85:21440
Sec-WebSocket-Key: dCgnP2FghlLKytuX3+2A/Q==
Sec-WebSocket-Version: 13
Connection: Upgrade
-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Server: websocket-sharp/1.0
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: 4stPQlqdux2KWrezuf99JJGhUTg=
-----------------------
send: b"\x81\xfe\x00\xeb!'\xb1\xc1Z\x05\xf9\xa0OC\xdd\xa4bH\xd5\xa4\x03\x1d\x91\xe3`\x17\x81\xf0\x03\x0b\x91\xe3lH\xd5\xb4MB\xff\xa0LB\x93\xfb\x01\x05\xe1\xad@^\xd4\xb3\x03\x0b\x91\xe3lB\xc5\xa9NC\xff\xa0LB\x93\xfb\x01\x05\xfd\xaeFN\xdf\x9egH\xc3\x85DE\xc4\xa6uB\xc2\xb5\x03\x0b\x91\xe3qF\xc3\xb5OB\xc3\x88E\x05\x8b\xe1\x03\x16\x81\xf1\x10\x05\x9d\xe1\x03t\xd4\xb3WB\xc3\x88E\x05\x8b\xe1\x03\x15\x80\xf5\x12\x1e\x93\xed\x01\x05\xe1\xad@^\xd4\xb3hC\x93\xfb\x01\x05\x93\xed\x01\x05\xe2\xa4RT\xd8\xaeO\x05\x8b\xe1\x03\x05\x9d\xe1\x03`\xd0\xacDq\xd4\xb3RN\xde\xafhC\x93\xfb\x01\x05\x80\xf3\x10\x15\x80\xf3\x10\x05\x9d\xe1\x03u\xd4\xb2NR\xc3\xa2Dq\xd4\xb3RN\xde\xafoF\xdc\xa4\x03\x1d\x91\xe3\x13\x15\x82\xf3@\x05\x9d\xe1\x03c\xd0\xb5@\x05\x8b\xe1z\x05\xf0\xacLh\xdf\xa4\x03z\xcc"
A001 返回信息:{'HandleCode': 'A001', 'PushKey': None, 'Status': 0, 'StatusMsg': '成功', 'Value': {'PlayerId': 'd873df59-6a0e-4ffd-8968-f2ed4882b398', 'Session': 'A6B0283E2CC39BB753C5F0198F035034', 'IsNewPlayer': True, 'PlayerName': 'AmmOne', 'UserId': '73094c2df83e4674a1140c01f693c1b5', 'RegisterTime': 1685515753, 'WorldLv': 83, 'WorldLvAddR': 0.0, 'ServerOpenDays': 63, 'ServerOpenDate': 1680319298, 'TimedSurpriseGiftVipLv': {}, 'TimedNewSurpriseGiftVipLv': {}, 'UseModelType': 1, 'IsInnerPlayer': 0, 'IfCanUseInnerCharge': 0, 'BlueVipInfo': {'BlueVipLevel': 0, 'IsBlueVip': False, 'IsBlueYearVip': False, 'IsSuperBlueVip': False}, 'ChatPartnerIdStr': '', 'PreRegistration': False}, 'TimeTick': 1685686533, 'Avatar': None, 'ExtValue': None}
====================================================================================================================================================================================
A002 参数发送:{'HandleCode': 'A002', 'ModuleName': 'Hero', 'MethodName': 'GetInfo', 'PartnerId': '1001', 'ServerId': '21439', 'PlayerId': 'd873df59-6a0e-4ffd-8968-f2ed4882b398', 'Session': 'A6B0283E2CC39BB753C5F0198F035034', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['12011110']}
A003 参数发送:{'HandleCode': 'A003', 'ModuleName': 'HeroMastery', 'MethodName': 'GetInfo', 'PartnerId': '1001', 'ServerId': '21439', 'PlayerId': 'd873df59-6a0e-4ffd-8968-f2ed4882b398', 'Session': 'A6B0283E2CC39BB753C5F0198F035034', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': []}
A002 返回信息:{'HandleCode': 'A002', 'PushKey': None, 'Status': 0, 'StatusMsg': '成功', 'Value': {'ModelId': 12011110, 'Fap': 45067, 'AttrContent': '{"Con":88,"Inte":90,"Str":73,"Conr":0,"Inter":0,"Strr":0,"Spd":0,"Ang":50,"AngLimit":0,"Atk":471,"Hp":5500,"Arm":275,"Hit":10200,"Miss":0,"Bog":338,"Blo":0,"Crit":0,"KangCrit":0,"Brk":0,"SklAdd":0,"CritTimes":0,"KangCritTimes":0,"Cont":0,"Free":0,"DmgAdd":0,"DmgDec":0,"HolyDmg":0,"CureAdd":0,"CurePer":0,"AtkPer":0,"HpPer":2000,"ArmPer":0,"SpdPer":0,"SklPerAdd":0,"SklPerDec":0,"SklEffect":0,"BeCuredPer":0,"AngRecover":0,"AngRecoverPer":0,"InteDmgAddPer":0,"InteDmgDecPer":0,"StrDmgAddPer":0,"StrDmgDecPer":0,"PlayerDmgAddPer":0,"PlayerDmgDecPer":0,"ComPerAdd":0,"ComPerDec":0,"PoisonDmgAddPer":0,"PoisonDmgDecPer":0}', 'FashionId': 30000029, 'ResourceId': 12011110, 'Lv': 1, 'Quality': 0, 'RedStar': 0, 'Exp': 0, 'TrainLv': 0, 'Star': 3, 'StepLv': 0, 'IsCombat': False, 'ResetFreeRecord': '', 'ControllPetModelId': 0, 'TalentPage': 1, 'TalentId': 0, 'TrainInfo': [{'TrainPartNum': 1, 'IsActive': True}, {'TrainPartNum': 2, 'IsActive': True}, {'TrainPartNum': 3, 'IsActive': True}, {'TrainPartNum': 4, 'IsActive': True}], 'PrInfo': {}, 'ArtifactInfo': {'Stars': 1, 'SkillList': '', 'SkillInfo': []}, 'SkillInfo': [{'SkillId': 111101, 'Lv': 1}], 'IsOpenHeroRoad': False, 'HeroRoadInfo': None, 'HeroTupoInfo': None, 'ChuanGongInfo': [], 'XinfaInfo': [], 'InheritStatus': False}, 'TimeTick': 1685686533, 'Avatar': {'MainSlotInfo': [{'ModelId': 12011110, 'Fap': 45067, 'AttrContent': '{"Con":88,"Inte":90,"Str":73,"Conr":0,"Inter":0,"Strr":0,"Spd":0,"Ang":50,"AngLimit":0,"Atk":471,"Hp":5500,"Arm":275,"Hit":10200,"Miss":0,"Bog":338,"Blo":0,"Crit":0,"KangCrit":0,"Brk":0,"SklAdd":0,"CritTimes":0,"KangCritTimes":0,"Cont":0,"Free":0,"DmgAdd":0,"DmgDec":0,"HolyDmg":0,"CureAdd":0,"CurePer":0,"AtkPer":0,"HpPer":2000,"ArmPer":0,"SpdPer":0,"SklPerAdd":0,"SklPerDec":0,"SklEffect":0,"BeCuredPer":0,"AngRecover":0,"AngRecoverPer":0,"InteDmgAddPer":0,"InteDmgDecPer":0,"StrDmgAddPer":0,"StrDmgDecPer":0,"PlayerDmgAddPer":0,"PlayerDmgDecPer":0,"ComPerAdd":0,"ComPerDec":0,"PoisonDmgAddPer":0,"PoisonDmgDecPer":0}'}, {'ModelId': 12011604, 'Fap': 43704, 'AttrContent': '{"Con":89,"Inte":73,"Str":89,"Conr":0,"Inter":0,"Strr":0,"Spd":0,"Ang":50,"AngLimit":0,"Atk":455,"Hp":5256,"Arm":259,"Hit":10000,"Miss":100,"Bog":0,"Blo":0,"Crit":300,"KangCrit":0,"Brk":0,"SklAdd":0,"CritTimes":0,"KangCritTimes":0,"Cont":0,"Free":0,"DmgAdd":0,"DmgDec":0,"HolyDmg":0,"CureAdd":0,"CurePer":0,"AtkPer":0,"HpPer":2000,"ArmPer":0,"SpdPer":0,"SklPerAdd":0,"SklPerDec":0,"SklEffect":0,"BeCuredPer":0,"AngRecover":0,"AngRecoverPer":0,"InteDmgAddPer":0,"InteDmgDecPer":0,"StrDmgAddPer":0,"StrDmgDecPer":0,"PlayerDmgAddPer":0,"PlayerDmgDecPer":0,"ComPerAdd":0,"ComPerDec":0,"PoisonDmgAddPer":0,"PoisonDmgDecPer":0}'}, {'ModelId': 12012107, 'Fap': 43469, 'AttrContent': '{"Con":86,"Inte":84,"Str":87,"Conr":0,"Inter":0,"Strr":0,"Spd":0,"Ang":50,"AngLimit":0,"Atk":467,"Hp":5159,"Arm":256,"Hit":10000,"Miss":0,"Bog":400,"Blo":0,"Crit":200,"KangCrit":0,"Brk":0,"SklAdd":0,"CritTimes":0,"KangCritTimes":0,"Cont":0,"Free":0,"DmgAdd":0,"DmgDec":0,"HolyDmg":0,"CureAdd":0,"CurePer":0,"AtkPer":0,"HpPer":2000,"ArmPer":0,"SpdPer":0,"SklPerAdd":0,"SklPerDec":0,"SklEffect":0,"BeCuredPer":0,"AngRecover":0,"AngRecoverPer":0,"InteDmgAddPer":0,"InteDmgDecPer":0,"StrDmgAddPer":0,"StrDmgDecPer":0,"PlayerDmgAddPer":0,"PlayerDmgDecPer":0,"ComPerAdd":0,"ComPerDec":0,"PoisonDmgAddPer":0,"PoisonDmgDecPer":0}'}, {'ModelId': 12012503, 'Fap': 43869, 'AttrContent': '{"Con":87,"Inte":94,"Str":70,"Conr":0,"Inter":0,"Strr":0,"Spd":0,"Ang":50,"AngLimit":0,"Atk":440,"Hp":5395,"Arm":268,"Hit":10000,"Miss":0,"Bog":0,"Blo":0,"Crit":0,"KangCrit":700,"Brk":0,"SklAdd":0,"CritTimes":0,"KangCritTimes":0,"Cont":0,"Free":0,"DmgAdd":0,"DmgDec":0,"HolyDmg":0,"CureAdd":0,"CurePer":0,"AtkPer":0,"HpPer":2000,"ArmPer":0,"SpdPer":0,"SklPerAdd":0,"SklPerDec":0,"SklEffect":0,"BeCuredPer":0,"AngRecover":0,"AngRecoverPer":0,"InteDmgAddPer":0,"InteDmgDecPer":0,"StrDmgAddPer":0,"StrDmgDecPer":0,"PlayerDmgAddPer":0,"PlayerDmgDecPer":0,"ComPerAdd":0,"ComPerDec":0,"PoisonDmgAddPer":0,"PoisonDmgDecPer":0}'}, {'ModelId': 12013113, 'Fap': 67502, 'AttrContent': '{"Con":93,"Inte":74,"Str":98,"Conr":0,"Inter":0,"Strr":0,"Spd":0,"Ang":50,"AngLimit":0,"Atk":571,"Hp":8502,"Arm":413,"Hit":10000,"Miss":0,"Bog":0,"Blo":500,"Crit":0,"KangCrit":0,"Brk":0,"SklAdd":0,"CritTimes":0,"KangCritTimes":0,"Cont":0,"Free":0,"DmgAdd":0,"DmgDec":0,"HolyDmg":0,"CureAdd":0,"CurePer":0,"AtkPer":0,"HpPer":6000,"ArmPer":0,"SpdPer":0,"SklPerAdd":0,"SklPerDec":0,"SklEffect":0,"BeCuredPer":0,"AngRecover":0,"AngRecoverPer":0,"InteDmgAddPer":0,"InteDmgDecPer":0,"StrDmgAddPer":0,"StrDmgDecPer":0,"PlayerDmgAddPer":0,"PlayerDmgDecPer":0,"ComPerAdd":0,"ComPerDec":0,"PoisonDmgAddPer":0,"PoisonDmgDecPer":0}'}], 'PvpChampionIsAutoBet': False, 'StrategyInfo': [{'StrategyId': 22011501, 'Mastery': 0, 'EntryIdLvStr': '', 'SkillIdLvStr': '', 'ResetTime': 946656000, 'UpdateTime': '2023-05-31T14:49:14', 'EntryIdLvDict': []}, {'StrategyId': 22011502, 'Mastery': 0, 'EntryIdLvStr': '', 'SkillIdLvStr': '', 'ResetTime': 946656000, 'UpdateTime': '2023-05-31T14:49:14', 'EntryIdLvDict': []}, {'StrategyId': 22011503, 'Mastery': 0, 'EntryIdLvStr': '', 'SkillIdLvStr': '', 'ResetTime': 946656000, 'UpdateTime': '2023-05-31T14:49:14', 'EntryIdLvDict': []}, {'StrategyId': 22012001, 'Mastery': 0, 'EntryIdLvStr': '', 'SkillIdLvStr': '', 'ResetTime': 946656000, 'UpdateTime': '2023-05-31T14:49:14', 'EntryIdLvDict': []}, {'StrategyId': 22012002, 'Mastery': 0, 'EntryIdLvStr': '', 'SkillIdLvStr': '', 'ResetTime': 946656000, 'UpdateTime': '2023-05-31T14:49:14', 'EntryIdLvDict': []}]}, 'ExtValue': [{'PushKey': 'Push_MergedAdvertisingInfo', 'Value': {}}, {'PushKey': 'Push_MergedAdvertisingInfo', 'Value': {}}, {'PushKey': 'Push_SpecialActivityInfo', 'Value': {'SpecialActivityMainInfo': {'MainId': 3, 'MainName': '新服活动', 'EndDate': 10320192000, 'SubModuleId': 42100}, 'SpecialActivityOpenInfo': [{'Id': 3000001, 'SpecialType': 1, 'Name': '兑换商店(2.0)', 'SubModuleId': 42102, 'BeginDate': 1680278400, 'EndDate': 1681142400}, {'Id': 3000002, 'SpecialType': 6, 'Name': '登录有礼(2.0)', 'SubModuleId': 42101, 'BeginDate': 1680278400, 'EndDate': 1680969600}, {'Id': 3000003, 'SpecialType': 7, 'Name': '开服开欢(2.0)', 'SubModuleId': 42104, 'BeginDate': 1680278400, 'EndDate': 1680883200}, {'Id': 3000004, 'SpecialType': 5, 'Name': '全服目标(2.0)', 'SubModuleId': 42105, 'BeginDate': 1680278400, 'EndDate': 1680883200}, {'Id': 3000006, 'SpecialType': 9, 'Name': '招募有礼', 'SubModuleId': 42106, 'BeginDate': 1680278400, 'EndDate': 1680883200}]}}, {'PushKey': 'Push_ShowZhiBo', 'Value': False}]}
send: b'\x81\xfe\x01$\xfd\x95\xec\xf3\x86\xb7\xa4\x92\x93\xf1\x80\x96\xbe\xfa\x88\x96\xdf\xaf\xcc\xd1\xbc\xa5\xdc\xc1\xdf\xb9\xcc\xd1\xb0\xfa\x88\x86\x91\xf0\xa2\x92\x90\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\x93G\x87c\xf0M\x97r\x93Q\x87c\xfc\x12\xc34\xdd\x18\xe9 \xdc\x18\x85{\x91_\xef$\xc3\x12\xea \xc2\t\xc23\xc8_\x8ba\x930\xc25\xd9\x12\xc3\x0f\xd0\x10\xc2c\x8b]\x85\x06\xd4\t\xee/\xd7\x12\x85m\x91_\xf7 \xc3\t\xc9$\xc34\xc3c\x8b]\x85p\x81M\x96c\x9d]\x85\x12\xd4\x0f\xd1$\xc34\xc3c\x8b]\x85s\x80I\x94x\x93Q\x87c\xe1\x11\xc68\xd4\x0f\xee%\x93G\x87c\xd5E\x90r\xd5\x1b\x92x\x9cK\xc6q\xd4P\x93'\xd7\x19\x8ay\x88K\x9fl\xd7O\xc2%\x85E\x9fs\xd3N\x9ey\x93Q\x87c\xe2\x18\xd42\xd8\x12\xc9c\x8b]\x85\x00\x87?\x97s\x89N\xe2s\xf2>\x94x\xf3?\x90t\x82>\x92\x07\x81L\x9ey\xf7M\x94t\x81N\x93c\x9d]\x85\x06\xd0\x10\xc2\x17\xd4\x0f\xd4(\xde\x13\xee%\x93G\x87c\x80O\x96s\x80O\x96c\x9d]\x85\x13\xd4\x0e\xc84\xc3\x1e\xc2\x17\xd4\x0f\xd4(\xde\x13\xe9 \xdc\x18\x85{\x91_\x95s\x82O\xc6c\x9d]\x85\x05\xd0\t\xc6c\x8b]\xfc\x1c\xcc"
A003 返回信息:{'HandleCode': 'A003', 'PushKey': None, 'Status': 0, 'StatusMsg': '成功', 'Value': {'MasteryInfoDic': {}, 'MasteryPoint': 0, 'LastUpProfession': 0, 'IsAllPracticeMastery': False}, 'TimeTick': 1685686533, 'Avatar': {'PvpChampionIsAutoBet': False}, 'ExtValue': None}
send: b'\x89\x80m\x98\x1f\xe8'
●WebSocketApp解析
WebSocketApp发送请求后,代码会一直运行下去,框架会不断给服务器发送心跳,若想查看服务器推送信息把on_message()方法中的pass注释了,打印信息即可,这个框架是模拟客户端请求。