且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Coinbase pro web socket获取货币的当前价格

更新时间:2023-11-30 12:42:34

它正在运行,但您同时收到 type='heartbeat' 和 type='ticker' 消息,并且它们异步发送到您的回调函数.因此,您必须等待回调接收到股票代码消息,然后才能尝试运行处理股票代码的代码.

It is working, but you get both type='heartbeat' and type='ticker' messages, and they are asynchronuosly sent to your callback function. So you must wait for the callback to receive a ticker message before trying to run the code that processes the ticker.

const websocket = new CoinbasePro.WebsocketClient(
  ["BTC-EUR"],
  "wss://ws-feed.pro.coinbase.com",
  null, // <-- you need to put your API key in
  {
    channels: ['ticker']
  }
);
websocket.on('message',data=>data.type==='ticker'&&xPrice=data.price&&console.log(data.price, data))
                             // (only want to see ticker messages)
                             // you will receive heartbeat (keep-alive) and ticker messages
                             // asynchronous callback will send data when it is available
                             // you must wait for data to be available and act on it