代码之家  ›  专栏  ›  技术社区  ›  BabbevDan

在Pusher客户端中接收事件

  •  3
  • BabbevDan  · 技术社区  · 8 年前

    我正在尝试连接到 BitStamp Websocket API 使用Python。

    然而,我在任何地方都找不到一个像样的教程或过程解释。

    我需要的是收到实时报价单。

    我试着用 this 图书馆,但我没有收到任何实价。我想我可能错过了一些东西,因为我对WebSocket是新手。

    这是我的代码:

    import pusherclient
    import sys
    
    # Add a logging handler so we can see the raw communication data
    import logging
    root = logging.getLogger()
    root.setLevel(logging.INFO)
    ch = logging.StreamHandler(sys.stdout)
    root.addHandler(ch)
    
    global pusher
    
    # We can't subscribe until we've connected, so we use a callback handler
    # to subscribe when able
    def connect_handler(data):
        channel = pusher.subscribe('live_trades')
        channel.bind('trade', callback)
    
    appkey = "de504dc5763aeef9ff52"
    pusher = pusherclient.Pusher(appkey)
    pusher.connection.bind('pusher:connection_established', connect_handler)
    pusher.connect()
    
    print("finished")
    

    当我运行这段代码时,我所看到的是-完成

    如何接收价格的实时更新?

    1 回复  |  直到 8 年前
        1
  •  2
  •   BabbevDan    8 年前

    在代码末尾添加一个while循环:

    while True:
        time.sleep(1)