Unity/스크립트

웹서버 구현없이 클라이언트에서 실시간 처리를 해야할때

빨강꼬마 2016. 4. 8. 18:13

ios는 상관없지만.. android의 경우 시간을 사용자 임의로 변경할 수 있기 때문에, 시간과 관련하여 컨텐츠의 변경이 필요할 경우 정확한 시간의 정보가 필요함.


이럴때, 미국 국립표준 연구소의 시간을 받아와서 사용하는 방법이 있다.


출처는 - http://stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet 요기

실제 수정해서 사용한 코드는 다음과 같음.

TcpClient tcpClient = new TcpClient("time.nist.gov", 13);

StreamReader sr = new StreamReader(tcpClient.GetStream());

        

// 형태 57486 16-04-08 08:53:18 50 0 0 737.0 UTC(NIST) * 

string readData = sr.ReadToEnd();

// 형태 16-04-08 08:57:07

string _time = readData.Substring(readData.IndexOf(" ") + 1, 17);


// 대한민국은 UTC 기준 +9시간.

Datetime currentTime = Convert.ToDateTime(_time).AddHours(9);


// Debug.Log("현재 시간 : " + currentTime.ToString("yyyy-MM-dd HH:mm:ss"));