Search Unity

Question Is WaitForSecondsRealtime Reliable in WebGL?

Discussion in 'Web' started by Gordon_G, Feb 20, 2023.

  1. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    Hi, we want to make sure our app pauses when the browser/tab becomes inactive (Run in Background = false in player settings). At the same time, we need to refresh our app data if more than 24 hours have elapsed since the last refresh after the app becomes active again.

    Is WaitForSecondsRealtime a reliable way to trigger that refresh across mobile and desktop browsers?

    That is, if the app is suspended and has a coroutine running that uses yield WaitForSecondsRealtime, does it use an accurate elapsed time once the app resumes?

    Thanks for your insights!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,937
    Actually, you are better off either comparing the device time on enter/exit background or when connected to a network, check a time server just in case the user changed the time settings (or there was a daylight savings time change).
     
  3. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    I can't find anything that tells you the app has become active again. Javascript proved to be unreliable in my extensive testing in this regard, and there is nothing I can find in the Unity documentation that says anything about it.

    Tell me if I am wrong, but there are no messages sent if the network becomes available. If not, then to test if the network was active would require regular polling of some kind. That would be more expensive that what I am doing now.

    My current strategy works and is sound - I was specifically looking for answers on how WaitForSecondsRealTime behaves. If it factors in the fact that an app may have paused for a number of hours, then I could increase the interval and check 24 hours later.

    We are not worried about people mucking around with their system time and will not affect the data they get. Also we provide a button the player can use to refresh anytime. The reason for the timer is to prompt them to refresh. We run a daily, day-of-year quiz (the sever determines which day of year it is) and the player knows to get the current quiz they need to refresh. We want to prompt them to do so.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,937
    Yeah sorry, I forgot to consider that this is a WebGL app. You may have to rely on browser events instead, no idea what kind are available though, if any.

    You could check time on the server side if you have a way of identifying each client. You could then check the time diff between the current and last message and if too much time has elapsed, notify the app that it needs to synch time.

    As to the behaviour of realtime seconds: just test and time it to see if there is any time diff after say 10 minutes in the background. ;)