【问题标题】:Subscribe to bitFlyer WebSocket订阅 bitFlyer WebSocket
【发布时间】:2018-04-28 13:45:20
【问题描述】:

我已经建立了与多个加密货币交易所的 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 示例。

非常感谢您在发送正确消息方面的任何帮助。

【问题讨论】:

    标签: python websocket


    【解决方案1】:

    我认为您发送的消息格式错误,请查看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()
    

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 2021-04-22
      • 1970-01-01
      • 2019-09-16
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多