Sign In Register

How can we help you today?

Start a new topic
Answered

[UnitySDK]Ping

Hello,

is it possible for client-sdk to get somehow ping time to the database? Thats important for us to know if some people have problems with connection to database(for example because of long distance in continent or strange NAT settings)


Best Answer

 Hi Marcin,


Please have a look at the example here, which covers pinging the server, getting a roundtip time and syncing Gametime accross different time zones using Gamesparks Real Time Api.


-If you wish to ping an ip address of your choice from Unity:


sing UnityEngine;
using System.Collections;

public class Check_InternetConnection : MonoBehaviour {

  public bool isConnectedToInternet = false;
  public float maxTime = 2.0F;
  public float timeTaken = 0.0F;

  void Awake()
  {
  StartCoroutine("ConnectionTest");
  }

  IEnumerator ConnectionTest()
  {

  while (true)
  {
  yield return new WaitForSeconds(2f);// the interval between ping attempts

  Ping testPing = new Ping("8.8.8.8");//Google - or Ip of your choice.
  Debug.Log("Testing......" + testPing.ToString());

  timeTaken = 0.0F;

  while (!testPing.isDone)
  {

  timeTaken += Time.deltaTime;


  if (timeTaken > maxTime)
  {
  // if time has exceeded the maxtime, break out and return false
  isConnectedToInternet = false;
  break;
  }

  yield return null;
  }

  if (timeTaken <= maxTime)
  {
  isConnectedToInternet = true;
  }
  yield return null;
  }
  }

}


Thanks,

-Dave.

1 Comment

Answer

 Hi Marcin,


Please have a look at the example here, which covers pinging the server, getting a roundtip time and syncing Gametime accross different time zones using Gamesparks Real Time Api.


-If you wish to ping an ip address of your choice from Unity:


sing UnityEngine;
using System.Collections;

public class Check_InternetConnection : MonoBehaviour {

  public bool isConnectedToInternet = false;
  public float maxTime = 2.0F;
  public float timeTaken = 0.0F;

  void Awake()
  {
  StartCoroutine("ConnectionTest");
  }

  IEnumerator ConnectionTest()
  {

  while (true)
  {
  yield return new WaitForSeconds(2f);// the interval between ping attempts

  Ping testPing = new Ping("8.8.8.8");//Google - or Ip of your choice.
  Debug.Log("Testing......" + testPing.ToString());

  timeTaken = 0.0F;

  while (!testPing.isDone)
  {

  timeTaken += Time.deltaTime;


  if (timeTaken > maxTime)
  {
  // if time has exceeded the maxtime, break out and return false
  isConnectedToInternet = false;
  break;
  }

  yield return null;
  }

  if (timeTaken <= maxTime)
  {
  isConnectedToInternet = true;
  }
  yield return null;
  }
  }

}


Thanks,

-Dave.

Login to post a comment