1

I am using Unity3D to create a game.

i need to get NTP time in order players not to cheat by setting their computer time to the future in order to get bonuses.

I can get the time for desktop application but when i switch to Web Player it gets blocked.

Any suggestions?

My code is this :

static string[] servers = new string[] {
"time.nist.gov",
"time-b.nist.gov",
"time-a.nist.gov",
"nist1-ny.ustiming.org",
"nist1-chi.ustiming.org",
"ntp-nist.ldsbc.edu",
"nist1-la.ustiming.org" 
};

public static DateTime GetNISTDate(){
for (int i = 0; i < 7; i++){
try
{
StreamReader reader = new StreamReader(new System.Net.Sockets.TcpClient(servers[i], 13).GetStream());
serverResponse = reader.ReadToEnd();
Debug.Log(serverResponse);
reader.Close();
// Check to see that the signature is there
if (serverResponse.Length > 47 && serverResponse.Substring(38, 9).Equals("UTC(NIST)")) {
// Parse the date
int jd = int.Parse(serverResponse.Substring(1, 5));
int yr = int.Parse(serverResponse.Substring(7, 2));
int mo = int.Parse(serverResponse.Substring(10, 2));
int dy = int.Parse(serverResponse.Substring(13, 2));
int hr = int.Parse(serverResponse.Substring(16, 2));
int mm = int.Parse(serverResponse.Substring(19, 2));
int sc = int.Parse(serverResponse.Substring(22, 2));
if (jd > 51544)
yr += 2000;
else
yr += 1999;
date = new DateTime(yr, mo, dy, hr, mm, sc);
Debug.Log(date);
break;
}
}
catch{
}
}
return date;
}
Brent Worden
  • 10,624
  • 7
  • 52
  • 57
Athorivos
  • 63
  • 6

1 Answers1

2

I'm not familiar with Unity Web Player, but that sure sounds like a same-origin policy issue.

You may need to fetch the time from the server your code is being loaded from instead (which might be as simple as making an HTTP request and parsing the Date header; just remember to disable caching).

Using you own server as a time source is generally a better practice anyway — public NTP servers, like those operated by NIST are operated voluntarily and free of charge by their owners, and used by millions of people across the Internet; it's simply good manners not to place any more load on them than you have to.

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153