From 4c00adebbda8d3a93cfa1a633a9493a6a2d24bec Mon Sep 17 00:00:00 2001 From: liminqi <736361931@qq.com> Date: Wed, 24 May 2023 05:31:46 +0000 Subject: [PATCH] =?UTF-8?q?update=20Demos/Python/ws.py.=20=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E4=BA=86=E5=BC=82=E6=AD=A5=EF=BC=8C=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=9C=89=E6=97=B6=E5=80=99=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liminqi <736361931@qq.com> --- Demos/Python/ws.py | 133 +++++++++++++++++++++++++++++---------------- 1 file changed, 87 insertions(+), 46 deletions(-) diff --git a/Demos/Python/ws.py b/Demos/Python/ws.py index 344849c..647f440 100644 --- a/Demos/Python/ws.py +++ b/Demos/Python/ws.py @@ -1,48 +1,89 @@ -from websocket import connection +import asyncio import json +from jsonpath import jsonpath +import websockets -def ck(data):#type2 - txt=data.get("Data") - load_data=json.loads(txt) - print("感谢"+load_data.get("Content")) - -def welcome(data):#type3 - json1=data.get("Data") - Nickname=json.loads(json1) - json2=Nickname.get("User") - print("欢迎:"+json2["Nickname"]) - -def thank(data):#type5 - txt=data.get("Data") - load_data=json.loads(txt) - print("感谢"+load_data.get("Content")) - -def check(data):#type6 - txt=data.get("Data") - load_data=json.loads(txt) - print(load_data.get("Content")) - -def main(): - try: - ws = create_connection("ws://127.0.0.1:8888") - ws.send("token") - while(True): - result = ws.recv() - load_data=json.loads(result) - type=load_data.get("Type")#标签类型 - if type==1:#1用户发言 - msg(load_data) - elif type==2:#2用户点赞 - ck(load_data) - elif type==3:#3用户入房 - welcome(load_data) - elif type==4:#用户关注 - print(str(load_data)) - elif type==5:#5用户礼物 - thank(load_data) - elif type==6:#6人数统计 - check(load_data) - ws.close() - except: - ws.close -main() \ No newline at end of file +# 1用户发言#2用户点赞#3用户入房#4关注主播#5用户礼物#6人数统计 +''' +str(fid[0:2])=="403": #字符串提取方便自定义命令 +''' + + +def msg(data): + global close + load_json_data = json.loads(data.get("Data")) + 用户名 = jsonpath(load_json_data, '$.User.Nickname') + 用户等级 = jsonpath(load_json_data, '$.User.PayLevel') + 粉丝团 = jsonpath(load_json_data, '$.User.FansClub.ClubName') + 用户发言 = jsonpath(load_json_data, '$.Content') + if 用户发言[0] == "#关闭": + close = False + else: + print(用户名[0]) + print(用户等级[0]) + + +def praise(data): # type2 + load_json_data = json.loads(data.get("Data")) + 用户点赞 = jsonpath(load_json_data, '$.Content') + print("感谢" + 用户点赞[0]) + + +def welcome(data): # type3 + load_json_data = json.loads(data.get("Data")) + 用户名 = jsonpath(load_json_data, '$.User.Nickname') + 粉丝团 = jsonpath(load_json_data, '$.User.FansClub.ClubName') + if 粉丝团[0] == "Reset": + print("欢迎粉丝:" + 用户名[0]) + else: + print("欢迎:" + 用户名[0]) + + +def thank(data): # type5 + load_json_data = json.loads(data.get("Data")) + 粉丝团 = jsonpath(load_json_data, '$.User.FansClub.ClubName') + 用户送礼 = jsonpath(load_json_data, '$.Content') + # txt1 = txt.replace("主播", "黎公子") #字符串替换功能方便拓展功能 + if 粉丝团[0] == "Reset": + print("感谢老板:" + 用户送礼[0]) + else: + print("感谢:" + 用户送礼[0]) + + +def check(data): # type6 + load_json_data = json.loads(data.get("Data")) + 房间统计 = jsonpath(load_json_data, '$.Content') + print(房间统计[0]) + + +def check_json(json_data): + Token = json_data.get("Type") # 标签类型 + if Token == 1: # 1用户发言 + msg(json_data) + elif Token == 2: # 2用户点赞 + praise(json_data) + elif Token == 3: # 3用户入房 + welcome(json_data) + elif Token == 4: # 感谢关注 + print(str(json_data)) + elif Token == 5: # 5用户礼物 + thank(json_data) + elif Token == 6: # 6人数统计 + check(json_data) + else: + print(json_data) + + +async def main(): + global close + async with websockets.connect("ws://127.0.0.1:8888/", ping_interval=None) as ws: + await ws.send("token") + close = True + while close is True: + result = await ws.recv() + check_json(json.loads(result)) + await ws.close() + + +close = True +asyncio.run(main()) \ No newline at end of file -- Gitee