Search Unity

[Help] How to Request real time from server / cloud on mobile device

Discussion in 'Multiplayer' started by guzzo, Feb 24, 2015.

  1. guzzo

    guzzo

    Joined:
    Feb 20, 2014
    Posts:
    79
    Hi. I am developing a game in which i need to know the real time for some calculations. Does anybody know how to get the current date/time from the cloud or server on mobile devices (especially on Android). I am currently using System.UtcNow, but it is easy to cheat.

    Thanks a lot!
     
  2. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Find a website that offers an HTTP response with the time. You can send a get request to the server, and read the response. It can all be done with Unity.
     
  3. guzzo

    guzzo

    Joined:
    Feb 20, 2014
    Posts:
    79
    Thanks, I will try it.
     
  4. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    The standard protocol for time synchronization is NTP, which is much cheaper to query. Here's a StackOverflow question containing a small C# NTP query function:

    http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c

    Bear in mind that although the protocol is relatively cheap, NTP servers can suffer horrific congestion. So:
    * Consider using a pool such as time.nist.gov rather than a specific server
    * Read the terms of service and make sure your app complies
    * As with any UDP-based protocol, for the initial request if you don't receive a response then you do need to retry, but wait at least five seconds before doing so; if you still don't receive a response, wait longer and longer each time you retry
    * For subsequent requests - if you really need them - wait as long as you can afford between requests. If possible, though, trust the device's internal clock, e.g. only query network time once and note the difference between the internal clock and network time, then apply that difference to the internal clock for all future time requirements.
     
  5. guzzo

    guzzo

    Joined:
    Feb 20, 2014
    Posts:
    79
    That was really useful. Thanks gfoot.
     
  6. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    I was using the stackoverflow answer and it was working well, but the App just got rejected by Apple because it didn't meet the new requirement to support IPv6 only networks. Does anyone have a solution that works for both IPv4 and IPv6?