如图,写到这里开始不知道如何继续往下写了。。求教
import socket
import requests
from jsonpath import jsonpath
def ChatSteam(session: requests.session() = None):
# 调用Steam API 以获取Steam的 WebSocket地址
response = requests.get(
"https://api.steampowered.com/ISteamDirectory/GetCMList/v1/?cellid=0").json()
serverlist_websockets = jsonpath(response, "$..serverlist_websockets[*]")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 生成套接字对象
for web_socket_url in serverlist_websockets:
HOST, PORT = web_socket_url.split(":")
# 遍历检查WebSocket地址是否可用
try:
s.connect((HOST, int(PORT))) # 创建套接字链接
# 得到可以成功链接的websocket地址
print(f"ChatSteam Websocket 连接:{web_socket_url} 成功!")
break
except Exception as e:
print(
f"ChatSteam Websocket 连接:{web_socket_url} 错误!Error: {str(e)}")
response = session.get(
'https://steamcommunity.com/chat/clientjstoken').json()
print("➡1 response json :", response)
token = response['token']
m_steamID = response['steamid']
基于Monster 组和GPT的调写:
可以使用已经连接的套接字对象s,以及获得的token和m_steamID来实现Steam聊天消息的接收和发送。
步骤:
发送一个登录请求到Steam聊天服务器,告诉服务器您的Steam ID和Token。
等待从服务器返回的消息。可以使用socket.recv()函数接收消息,它将阻塞程序,直到收到数据为止。
处理从服务器返回的消息。Steam聊天消息使用JSON格式,因此需要解析JSON字符串以获取消息内容。