订阅bitFlyer WebSocket

时间:2018-04-28 13:45:21

标签: python websocket

我已经建立了与多个加密货币交换的websocket连接,但是我很难连接到bitFlyer。

我的代码如下:

import websocket
import json

def on_message(ws, message):
    msg = json.loads(message)
    print(msg)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    ws.send(json.dumps({"method":"subscribe", "channel":"lightning_executions_FX_BTC_JPY"}))

while True:   
    if __name__ == "__main__":
        #websocket.enableTrace(True)
        ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc",
                                    on_message=on_message,
                                    on_error=on_error,
                                    on_close=on_close)
        ws.on_open = on_open
        ws.run_forever()

我尝试了许多on_open()消息的变体,大多数都会导致### closed ### Invalid close opcode.错误。

不幸的是,他们的文档不包含位于HERE的Python示例。

非常感谢发送正确信息的任何帮助。

1 个答案:

答案 0 :(得分:3)

我相信您发送的邮件格式错误,请查看以下https://lightning.bitflyer.jp/docs/playgroundrealtime的引用,猜猜它会解决。

# pip install websocket-client
import websocket
import json
CHANNEL = "lightning_board_snapshot_<productCode>"

def on_message(ws, message):
    message = json.loads(message)
    if message["method"] == "channelMessage":
        print("{} {}".format(message["params"]["channel"], message["params"]["message"]))

def on_open(ws):
    ws.send(json.dumps({"method": "subscribe", "params": {"channel": CHANNEL}}))

if __name__ == "__main__":
    // note: reconnection handling needed.
    ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc",
                              on_message = on_message, on_open = on_open)
    ws.run_forever()