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

I'm working on a CCXT based app server on Node. I want to measure websocket connection latency bet

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745123717a4612566.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信