示例#1
0
        private static void FillRequestResult(UnityWebRequest request, ref OnlineTimeResult result)
        {
            if (!string.IsNullOrEmpty(request.error))
            {
                result.SetError(request.error, request.responseCode);
            }
            else
            {
                var dateHeader = request.GetResponseHeader("Date");
                if (!string.IsNullOrEmpty(dateHeader))
                {
                    DateTime serverTime;
                    var      success = TryGetDate(dateHeader, out serverTime);
                    if (success)
                    {
                        var onlineTimeUtc    = serverTime.ToUniversalTime();
                        var onlineSecondsUtc = onlineTimeUtc.Ticks / (double)TimeSpan.TicksPerSecond;
                        result.SetTime(onlineSecondsUtc, onlineTimeUtc);
                    }
                    else
                    {
                        result.SetError("Couldn't parse 'Date' response header value\n " + dateHeader, request.responseCode);
                    }
                }
                else
                {
                    result.SetError("Couldn't find 'Date' response header value!", request.responseCode);
                }
            }

            if (!result.success)
            {
                Debug.Log(FinalLogPrefix + "Online Time Retrieve error:\n" + result);
            }
        }
示例#2
0
        /// <summary>
        /// Receives UTC seconds from url. Runs asynchronously in coroutine.
        /// </summary>
        /// Automatically switches to the current domain when running in WebGL to avoid CORS limitation.
        /// <param name="uri">Absolute url to receive time from.
        /// Make sure this server has proper Date values in the response headers
        /// (almost all popular web sites are suitable).</param>
        /// <param name="callback">Delegate to call and pass OnlineTimeResult to.</param>
        /// <param name="method">Method to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.</param>
        public static IEnumerator GetOnlineTimeCoroutine(Uri uri, OnlineTimeCallback callback, RequestMethod method = RequestMethod.Head)
        {
#if ACTK_WEBGL_BUILD
            EnsureCurrentDomainUsed(ref uri);
#endif

            if (gettingOnlineTime)
            {
                yield return(CachedEndOfFrame);
            }

            gettingOnlineTime = true;

            var result = new OnlineTimeResult();

            using (var wr = GetWebRequest(uri, method))
            {
                yield return(wr.SendWebRequest());

                FillRequestResult(wr, ref result);
            }

            if (callback != null)
            {
                callback.Invoke(result);
            }

            gettingOnlineTime = false;
        }
示例#3
0
        /// <summary>
        /// Receives UTC seconds from url. Runs asynchronously.
        /// </summary>
        /// Automatically switches to the current domain when running in WebGL to avoid CORS limitation.
        /// <param name="uri">Absolute url to receive time from.
        /// Make sure this server has proper Date values in the response headers
        /// (almost all popular web sites are suitable).</param>
        /// <param name="method">Method to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.</param>
        /// <returns>OnlineTimeResult with UTC seconds or error.</returns>
        public static async Task <OnlineTimeResult> GetOnlineTimeTask(Uri uri, RequestMethod method = RequestMethod.Head)
        {
#if ACTK_WEBGL_BUILD
            EnsureCurrentDomainUsed(ref uri);
#endif
            while (gettingOnlineTime)
            {
                await Task.Delay(100);
            }

            gettingOnlineTime = true;

            var result = new OnlineTimeResult();

            using (var wr = GetWebRequest(uri, method))
            {
                var asyncOperation = wr.SendWebRequest();

                while (!asyncOperation.isDone)
                {
                    await Task.Delay(100);
                }

                FillRequestResult(wr, ref result);
            }

            gettingOnlineTime = false;

            return(result);
        }
示例#4
0
 private void OnOnlineTimeReceived(OnlineTimeResult result)
 {
     if (result.success)
     {
         lastOnlineSecondsUtc = result.onlineSecondsUtc;
     }
     else
     {
         lastOnlineSecondsUtc = -1;
         LastError            = ErrorKind.OnlineTimeError;
     }
 }