As a way to prevent cheating, I wanted to pull the current Date and Time from the net. So I tried this, and it works just fine in editor. However, when I deploy to WebGL, it gets nothing back in response. The code executes, but returns netTime as a null string. Again, in editor, it works exactly as intended and returns the date/time as a string. WebGL fails. Any ideas why? My understanding is that UnityWebRequest should be working with WebGL per the documentation (I’m using 5.5, it says implemented by 5.3)?
Tried multiple sites (besides MS), they all work locally, none work in WebGL. My guess atm is it works locally b/c there is no cross-domain issue (CORS), but once in browser, it’s hitting a CORS issue. I’m trying a CORS proxy to see if that fixes it.
Nope, even when I use a CORS proxy, it gets rid of the error returned, but I’m still getting absolutely nothing back. Sigh. Any ideas network knowledgeable folks? I’m amenable to alternate solutions if there’s a slicker way to get date/time from the net?
Yah it’s not CORS, I used https://crossorigin.me to see if that was the problem. It doesn’t solve it, I’m getting absolutely nothing back in WebGL, although it seems to work in editor and PC platform…
I double checked I’m not getting any error with myHttpWebRequest.error is .isError. No error, just null coming back on WebGL.
I even tried using the legacy WWW, and the exact same result. Null.
WWW www = new WWW("http://www.whattimeisit.com/");
yield return www;
www.text
www.text works fine in editor, properly grabs websites text, but in WebGL, nothing but null. True for WWW, true for UnityWebRequest. I’m out of ideas
I actually couldn’t get his NTP script to work at all. Even then, the issue isn’t that above method doesn’t work, it does! It just doesn’t work on WebGL!
That said, I finally got it to return me SOME text using WWW, so I’ll just go down that road and see if I can find a website without ridiculous text for me to parse Really should work with UnityWebRequest though…
Same problem here, when i try to get the date my scene freeze some sec (something like 40sec). I put a try / catch and it always goes in catch (i have internet)
Based on the information gathered here and there I wrote this static class that retrieves the internet time asynchronously (no scene freeze).
Call TimeManager.InitializeTime() at the start of your game and handle errors as you want. Then use TimeManager.UtcNow to get the current UTC time. If you want the local time add DateTimeStyles.AssumeUniversal as the last argument of DateTime.ParseExact (and modify the _utcStartTime name and initial value).
Note that this is not a great solution if you need the time for the internet very early in your code because we don’t know when the async method will finish. You could add some bool flag to know if the time was retrieved or not (or if something went wrong) to get around this issue though.
using System;
using System.Globalization;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
using UnityEngine;
public static class TimeManager
{
private static DateTime _utcStartTime = DateTime.UtcNow;
public static DateTime UtcNow => _utcStartTime.AddSeconds(Time.realtimeSinceStartup);
public static void InitializeTime()
{
GetUtcTimeAsync().WrapErrors();
}
private static async Task GetUtcTimeAsync()
{
try
{
var client = new TcpClient();
await client.ConnectAsync("time.nist.gov", 13);
using var streamReader = new StreamReader(client.GetStream());
var response = await streamReader.ReadToEndAsync();
var utcDateTimeString = response.Substring(7, 17);
_utcStartTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
catch
{
// Handle errors here
}
}
private static async void WrapErrors(this Task task)
{
await task;
}
}