最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to initiate PingPong in CCXT websockets under Node - Stack Overflow

programmeradmin1浏览0评论

I'm working on a CCXT based app server on Node. I want to measure websocket connection latency between Node and remote exchanges. For this purpose I'd like to initiate a ping right after websocket stream subscription and then repeat it periodically according to 'keepAlive' settings. Upon receipt of a reply pong I could calculate the roundtrip time.

I can override the handler of the pongs:

ex = new Exchange();
if(ex.handlePong)
{
   const original_handler = ex.handlePong.bind(ex);
   ex.handlePong = (ws, data) => { original_handler(ws, data); console.log("Pong at ", Date.now(), "after", ex.myPing); ... actual stuff here ...}
}

But I can't figure out how to send pings. The following attempts do not produce any effect:

if(!ex.myPing || Date.now() - ex.lastPong > 30000)
{
   ex.myPing = Date.now();
   for(const key in ex.clients)
   {
      if(key.startsWith("ws"))
      {
         ex.clients[key].connection.ping(); // ?data, mask, cb?
         // ex.clients[key].ping(); // doesn't seem to do anything
      }
   }
}

I intercept the pongs, but they look as arriving by server's own schedule, after the predefined interval. In other words, my pings either are not sent at all, or ignored for some reason, silently - there are no exceptions.

Probably my approach with "poking" the client object is incorrect, but it's just a result of digging into the source code and debugging, because I did not find any official info on how to do this.

Any suggestions on how to send "effective" pings (force the remote server to answer with pongs) or measure websocket latency in another way?

发布评论

评论列表(0)

  1. 暂无评论