using UnityEngine; using System.Collections; using System; using System.IO; using System.Net; using System.Text; using System.Threading; using UnityEngine.Networking; public class ServerTime : MonoBehaviour { #region Unity Singleton private static ServerTime _instance; public static ServerTime Instance { get { if (_instance == null) { _instance = FindObjectOfType(typeof(ServerTime)) as ServerTime; if (_instance == null) { AVDebug.LogError(string.Format("No gameObject with {0} component exists. Make sure to create a gameObject with {0} component", new System.Diagnostics.StackFrame().GetMethod().DeclaringType)); } } return _instance; } } void Awake() { if (_instance != null && _instance != this) { AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name)); Destroy(gameObject); return; } _instance = this; DontDestroyOnLoad(gameObject); StartCoroutine(LoadServerTimeRoutine()); } private IEnumerator LoadServerTimeRoutine() { string url = GameConstants.SERVER_BASE_URL + "serverTime.php"; var unityWebRequest = new UnityWebRequest(url); yield return new WaitForSeconds(3); //RetrieveServerTime(OnGetTime); StartCoroutine(AwaitRequest(unityWebRequest,Results)); } private void Results(long obj) { Debug.Log(obj); } private void Percentage(UnityWebRequest arg1, ulong arg2) { throw new NotImplementedException(); } private void OnGetTime(long l) { Debug.Log("OnGetTime " + l); } #endregion /// /// Returns the server time as a Unix Time Stamp, i.e. current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT), /// through the onComplete action. /// If there was an error, -1 is returned through the onComplete action /// public void RetrieveServerTime(Action onComplete) { AVDebug.Assert(onComplete != null, "onComplete must not be null when requesting server time"); StartCoroutine(RetrieveServerTimeCoroutine(onComplete)); } internal IEnumerator AwaitRequest(UnityWebRequest request, Action result) { WWW www = new WWW(GameConstants.SERVER_BASE_URL + "serverTime.php"); yield return www; if (www.error != null) { result(-1); } else { long serverTime; if (long.TryParse(www.text, out serverTime)) { result(serverTime); } else { result(-1); //parse error } } } private Action _onComplete; IEnumerator RetrieveServerTimeCoroutine(Action onComplete) { _onComplete = onComplete; Debug.Log("Start SEND SERVER TIME"); //downloadData(); string url = GameConstants.SERVER_BASE_URL + "serverTime.php"; UnityWebRequest webrequest = UnityWebRequest.Get(url); yield return webrequest.SendWebRequest(); Debug.Log("end SEND SERVER TIME"); if (webrequest.isNetworkError || webrequest.isHttpError) { print("Network Error"); // return Unkown Error IOS Mobile onComplete(-1); } else { Debug.Log(webrequest.downloadHandler.text); long serverTime; if (long.TryParse(webrequest.downloadHandler.text, out serverTime)) { onComplete(serverTime); } else { onComplete(-1); //parse error } } } void downloadData() { ThreadPool.QueueUserWorkItem(new WaitCallback(makeRequest)); } private void makeRequest(object a) { string url = GameConstants.SERVER_BASE_URL + "serverTime.php"; string result = ""; var request = (HttpWebRequest)WebRequest.Create(url); //Speed up request.Proxy = null; using (var response = (HttpWebResponse)request.GetResponse()) { var encoding = Encoding.GetEncoding(response.CharacterSet); using (var responseStream = response.GetResponseStream()) using (var reader = new StreamReader(responseStream, encoding)) result = reader.ReadToEnd(); } long serverTime = -1; UnityThread.executeInUpdate(() => { Debug.Log("end SEND SERVER TIME"); if (long.TryParse(result, out serverTime)) { _onComplete(serverTime); } else { _onComplete(-1); //parse error } }); } }