接收资产变动和聊天消息

为了能实时客户的聊天和资产消息,开发者需要建立一个websocket客户端,并且连接到服务器上。这里有一个介绍页面 guide.

  1. 使用文件gzip方式压缩.

  2. 要接收消息,一定发 LIST_PENDING_MESSAGES

go 语言代码片段

func ConnectMixinBlaze(token string) (*websocket.Conn, error) {
header := make(http.Header)
header.Add("Authorization", "Bearer "+token)
u := url.URL{Scheme: "wss", Host: "blaze.mixin.one", Path: "/"}
dialer := &websocket.Dialer{
Subprotocols: []string{"Mixin-Blaze-1"},
}
conn, _, err := dialer.Dial(u.String(), header)
if err != nil {
return nil, err
}
return conn, nil
}

python语言代码片段

def on_open(ws):
def run(*args):
print("ws open")
Message = {"id":str(uuid.uuid1()), "action":"LIST_PENDING_MESSAGES"}
Message_instring = json.dumps(Message)
fgz = StringIO()
gzip_obj = gzip.GzipFile(mode='wb', fileobj=fgz)
gzip_obj.write(Message_instring)
gzip_obj.close()
ws.send(fgz.getvalue(), opcode=websocket.ABNF.OPCODE_BINARY)
while True:
time.sleep(1)
thread.start_new_thread(run, ())
if __name__ == "__main__":
while True:
encoded = mixin_api_robot.genGETJwtToken('/', "", str(uuid.uuid4()))
#websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://blaze.mixin.one/",
on_message = on_message,
on_error = on_error,
on_close = on_close,
header = ["Authorization:Bearer " + encoded],
subprotocols = ["Mixin-Blaze-1"],
on_data = on_data)
ws.on_open = on_open
ws.run_forever()

然后可以开始分析服务器发来的消息。这里有一个介绍 guide.

go 完整例子

python 完整的例子