123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 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
- /// <summary>
- /// 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
- /// </summary>
- public void RetrieveServerTime(Action<long> onComplete)
- {
- AVDebug.Assert(onComplete != null, "onComplete must not be null when requesting server time");
- StartCoroutine(RetrieveServerTimeCoroutine(onComplete));
- }
- internal IEnumerator AwaitRequest(UnityWebRequest request, Action<long> 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<long> _onComplete;
- IEnumerator RetrieveServerTimeCoroutine(Action<long> 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
- }
- });
-
- }
- }
|