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

admin2025-04-20  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?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745123718a286300.html

最新回复(0)