Esempio n. 1
0
        private IEnumerator SendRequestCoroutine(UnityWebRequest request, Action <GbaseError, string> callback, bool dequeue = true)
        {
            GbaseAPI.Print("~~> " + request.method + " " + request.url);
            yield return(request.SendWebRequest());

            GbaseAPI.Print("<~~ " + request.method + " " + request.responseCode + " " + request.downloadHandler.text);

            if (request.responseCode == 0)
            {
                yield return(StartCoroutine(RepeatLastRequest(request, callback)));

                if (dequeue)
                {
                    _requestsQueue.Dequeue();
                }
            }
            else
            {
                if (dequeue)
                {
                    _requestsQueue.Dequeue();
                }
                if (request.isHttpError || request.isNetworkError)
                {
                    if (IsDeadSession(request))
                    {
                        GbaseAPI.Instance.Reauth(() =>
                        {
                            var lastRequest = new Request(request.method, request.url,
                                                          request.uploadHandler != null ? request.uploadHandler.data : new byte[0]);
                            SendRequest(lastRequest.www, callback);
                        });
                    }
                    else if (IsUnderMaintence(request))
                    {
                        yield return(StartCoroutine(RepeatLastRequest(request, callback, SERVER_UNDER_MAINTENCE_DELAY)));
                    }
                    else
                    {
                        if (callback != null)
                        {
                            callback(new GbaseError(request.downloadHandler.text), null);
                        }
                    }
                }
                else
                {
                    if (callback != null)
                    {
                        callback(null, request.downloadHandler.text);
                    }
                }
            }
        }
        private void Awake()
        {
            if (_instance == null)
            {
                _instance        = this;
                transform.parent = null;
                DontDestroyOnLoad(gameObject);

                _prefix = _localhost ? "local" : string.Format("{0}-{1}.{2}_", _projectName, _environment, _domainName);
                _url    = _localhost ? "http://localhost:8000/" : string.Format("https://{0}-{1}.{2}/", _projectName, _environment, _domainName);

                if (_networkManager == null)
                {
                    _networkManager = gameObject.AddComponent <NetworkManager>();
                }
                _networkManager.Init(_url, _hmacSecret, _platform.ToString(), _version);
            }
            else
            {
                Destroy(gameObject);
            }
        }
Esempio n. 3
0
        private IEnumerator RepeatLastRequest(UnityWebRequest req, Action <GbaseError, string> callback, float delay = -1)
        {
            if (delay > 0)
            {
                yield return(new WaitForSeconds(delay));
            }

            var request = new Request(req.method, req.url, req.uploadHandler.data).www;

            request.timeout = TIMEOUT_SECONDS;
            request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
            request.SetRequestHeader("X-Req-Seq", req.GetRequestHeader("X-Req-Seq"));
            request.SetRequestHeader("X-Platform-Version", _platformVersion);
            request.SetRequestHeader("X-Request-Sign", GetHmacSign(request));
            if (!string.IsNullOrEmpty(_unicorn))
            {
                request.SetRequestHeader("X-Unicorn", _unicorn);
            }

            GbaseAPI.Print("~~> " + request.method + " " + request.url);
            yield return(request.SendWebRequest());

            GbaseAPI.Print("<~~ " + request.method + " " + request.responseCode + " " + request.downloadHandler.text);

            if (request.responseCode == 0)
            {
                if (++_repeatCount >= MAX_REPEAT_COUNT)
                {
                    GbaseAPI.Instance.Quit();
                    if (callback != null)
                    {
                        callback(new GbaseError(0, "Connection problem"), null);
                    }
                }
                else
                {
                    yield return(StartCoroutine(RepeatLastRequest(request, callback)));
                }
            }
            else
            {
                if (request.isHttpError || request.isNetworkError)
                {
                    if (IsSequenceMismatch(request))
                    {
                        if (callback != null)
                        {
                            callback(null, "OK");
                        }
                    }
                    else if (IsUnderMaintence(request))
                    {
                        yield return(StartCoroutine(RepeatLastRequest(request, callback, SERVER_UNDER_MAINTENCE_DELAY)));
                    }
                    else
                    {
                        if (callback != null)
                        {
                            callback(new GbaseError(request.downloadHandler.text), null);
                        }
                    }
                }
                else
                {
                    if (callback != null)
                    {
                        callback(null, request.downloadHandler.text);
                    }
                }
            }
        }