Search Unity

Having Trouble Getting Current Time From My Website.

Discussion in 'Scripting' started by Ralem, Dec 2, 2015.

  1. Ralem

    Ralem

    Joined:
    Sep 26, 2013
    Posts:
    15
    I want the time to be uniform so that the player can't change their system time and cheat. So I'm trying to use php using my website. When I load the page the time is correct and changing. When I run it through Unity it ends up reading the same time over and over again. It debugs the time out as 1449006906 every time. I'm stuck and can't figure out what is happening.

    Code (csharp):
    1.  
    2. <?php
    3. print date("U", time());
    4. ?>
    5.  
    Code (csharp):
    1.  
    2. public class TEST : MonoBehaviour
    3. {
    4.     bool trigger = true;
    5.     void Update()
    6.     {
    7.         if (trigger) { StartCoroutine(getTime()); trigger = false; }
    8.     }
    9.  
    10.     private IEnumerator getTime()
    11.     {
    12.         WWW www = new WWW("http://ralemproductions.net/time.php");
    13.         yield return www;
    14.  
    15.         Debug.Log("Time on the server is now: " + www.text);
    16.         trigger = false;
    17.     }
    18. }
    19.  
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    So, I'm guessing the FIRST time you ran this test was at 9:55 this evening your local time? (that's the date of 1449006906 in unix time, which php uses)

    I'm betting the retrieved data was cached on your local machine, and every time you WWW uri the locally cached data is being loaded.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    Honestly, I don't use WWW very much... nothing against it, just so used to using .Net that I seldom touch it.

    Anyways, you could try using WebRequest. It offers more freedom than WWW, but it doesn't automatically load/parse the streamed data into the respective unity objects (though you can always create them from the stream that is returned via the various methods for texture/assetbundle/etc that allow creating them from a block of memory).

    Other downside is that it isn't accepted as a yield instruction either.

    https://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.110).aspx



    Also, here is a thread about iOS caching results:
    http://forum.unity3d.com/threads/www-class-get-not-refreshing-after-first-call.250700/

    A unity tech responds with some examples.
     
  4. Ralem

    Ralem

    Joined:
    Sep 26, 2013
    Posts:
    15
    I think its cached or something too but when I use someones elses php I found on a forum it seems to be fine. I went ahead and looked into doing WebRequest and I used this code from the site.
    Code (csharp):
    1.  
    2. public class TEST : MonoBehaviour
    3. {
    4.     bool trigger = true;
    5.     void Update()
    6.     {
    7.         if (trigger) { StartCoroutine(getTime()); trigger = false; }
    8.     }
    9.  
    10.     private IEnumerator getTime()
    11.     {
    12.         // Create a request for the URL.        
    13.         WebRequest request = WebRequest.Create("http://ralemproductions.net/time.php");
    14.         // If required by the server, set the credentials.
    15.         request.Credentials = CredentialCache.DefaultCredentials;
    16.         // Get the response.
    17.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    18.         // Display the status.
    19.         //Console.WriteLine(response.StatusDescription);
    20.         // Get the stream containing content returned by the server.
    21.         Stream dataStream = response.GetResponseStream();
    22.         // Open the stream using a StreamReader for easy access.
    23.         StreamReader reader = new StreamReader(dataStream);
    24.         // Read the content.
    25.         string responseFromServer = reader.ReadToEnd();
    26.         // Display the content.
    27.         Debug.Log(responseFromServer);
    28.         // Cleanup the streams and the response.
    29.         reader.Close();
    30.         dataStream.Close();
    31.         response.Close();
    32.  
    33.         yield return new WaitForSeconds(.5f);
    34.     }
    35. }
    36.  
    I was excited when I got it working. But was sad to realize the time was still 1449006906. Could it be my php file. Or maybe something to do with my web hosting?
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532