How to get players ping?

Hello!
Could someone please help me?
I want to show players’ ping as a part of debug panel. How can I get this value?

Hi @Oleksii_Dubrovskyi and welcome,

If you are asking about networking ping, there isn’t any API for handling that in PlayCanvas.

That would be part of the networking solution/library you are using. How are you handling that?

1 Like

Im new at JavaScript so Ive searched around the network and found a solution. Don`t know how good it is but hope this could help someone.

function ping(ip) {
  var img = new Image();
  var start = new Date().getTime();
  var endTime = new Date().getTime();
  var flag = false;
  var isCloseWifi = true;
  var hasFinish = false;
  var miliseconds = 0;

  img.onload = function() {
    if ( !hasFinish ) {
      flag = true;
      hasFinish = true;

      endTime = new Date().getTime();
      miliseconds = endTime - start;
      console.log('Ping ' + ip + ' success. ' + miliseconds);
    }
  };

  img.onerror = function() {
    if ( !hasFinish ) {
      if ( !isCloseWifi ) {
            flag = true;

            endTime = new Date().getTime();
            miliseconds = endTime - start;
            console.log('Ping ' + ip + ' success. ' + miliseconds);
      } else {
            console.log('network is not working!');
      }

      hasFinish = true;
    }
  };

  setTimeout(function(){
        isCloseWifi = false;
        console.log('network is working, start ping...');
    },2);

  img.src = 'https://' + ip;

  var timer = setTimeout(function() {
    if ( !flag ) {
      hasFinish = true;
      flag = false ;
      console.log('Ping ' + ip + ' fail. ');
    }
  }, 1500);
}