我正在使用这个Python代码连接到discord网关,基本上我需要发送一个Opcode 2 Identify,只是为了能够用discord API在一个频道上发送信息。
import websocket
import json
import pprint
ws = websocket.WebSocket()
# Connect to host url
ws.connect("wss://gateway.discord.gg/?v=6&encoding=json")
# Use ws.send() to send data to server
# Use ws.recv() to get the data sent from server
result = ws.recv()
print "Received: ",result
heartbeat = '{"op": 1,"d": 251}'
p = '{"token": "MY_BOT_TOKEN","properties": {"$os": "linux","$browser": "disco","$device": "disco" },"compress": false, "large_threshold": 250,"shard": [0, 1],"presence": {"game": {},"status": "online","since": null,"afk": false}}'
h = json.loads(heartbeat)
h_json = json.dumps(h)
p_load = json.loads(p)
p_json = json.dumps(p_load)
print(h_json)
ws.send(h_json)
# Use ws.close() to close the WebSocket handshake
result = ws.recv()
print "Received: ",result
ws.send(p_json)
result = ws.recv()
print "Received: ",result
这段代码所做的是:在操作码10之后发送心跳,从服务器接收操作码11,为操作码2识别发送json对象。
But the result is this:
Received: {"t":null,"s":null,"op":10,"d":{"heartbeat_interval":41250,"_trace":["gateway-prd-main-rskw"]}}
{"d": 251, "op": 1}
Received: {"t":null,"s":null,"op":11,"d":null}
Received:
问题是,在发送完json后,连接被关闭,我无法收到响应,我的请求有什么问题吗?