コード例 #1
0
        private WebRequestAsyncOperation SendWebRequest <T>(
            T requestType,
            string url,
            Action <UnityWebRequest> OnSuccess,
            Action <string> OnFail,
            int requestAttemps,
            int timeout) where T : IWebRequest
        {
            int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps);

            UnityWebRequest request = requestType.CreateWebRequest(url);

            request.timeout = timeout;

            WebRequestAsyncOperation resultOp = new WebRequestAsyncOperation(request);

            ongoingWebRequests.Add(resultOp);

            UnityWebRequestAsyncOperation requestOp = resultOp.webRequest.SendWebRequest();

            requestOp.completed += (asyncOp) =>
            {
                if (!resultOp.isDisposed)
                {
                    if (resultOp.webRequest.WebRequestSucceded())
                    {
                        OnSuccess?.Invoke(resultOp.webRequest);
                        resultOp.SetAsCompleted();
                    }
                    else if (!resultOp.webRequest.WebRequestAborted() && resultOp.webRequest.WebRequestServerError())
                    {
                        remainingAttemps--;
                        if (remainingAttemps > 0)
                        {
                            Debug.LogWarning($"Retrying web request: {url} ({remainingAttemps} attemps remaining)");
                            resultOp = SendWebRequest(requestType, url, OnSuccess, OnFail, remainingAttemps, timeout);
                        }
                        else
                        {
                            OnFail?.Invoke(resultOp.webRequest.error);
                            resultOp.SetAsCompleted();
                        }
                    }
                    else
                    {
                        OnFail?.Invoke(resultOp.webRequest.error);
                        resultOp.SetAsCompleted();
                    }
                }

                ongoingWebRequests.Remove(resultOp);
            };

            return(resultOp);
        }
コード例 #2
0
        private WebRequestAsyncOperation SendWebRequest <T>(
            T requestType,
            string url,
            DownloadHandler downloadHandler,
            Action <UnityWebRequest> OnSuccess,
            Action <UnityWebRequest> OnFail,
            int requestAttemps,
            int timeout,
            bool disposeOnCompleted,
            Dictionary <string, string> headers = null
            ) where T : IWebRequest
        {
            int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps);

            UnityWebRequest request = requestType.CreateWebRequest(url);

            request.timeout = timeout;
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    request.SetRequestHeader(item.Key, item.Value);
                }
            }

            if (downloadHandler != null)
            {
                request.downloadHandler = downloadHandler;
            }

            WebRequestAsyncOperation resultOp = new WebRequestAsyncOperation(request);

            resultOp.disposeOnCompleted = disposeOnCompleted;
            ongoingWebRequests.Add(resultOp);

            UnityWebRequestAsyncOperation requestOp = resultOp.webRequest.SendWebRequest();

            requestOp.completed += (asyncOp) =>
            {
                if (!resultOp.isDisposed)
                {
                    if (resultOp.webRequest.WebRequestSucceded())
                    {
                        OnSuccess?.Invoke(resultOp.webRequest);
                        resultOp.SetAsCompleted(true);
                    }
                    else if (!resultOp.webRequest.WebRequestAborted() && resultOp.webRequest.WebRequestServerError())
                    {
                        remainingAttemps--;
                        if (remainingAttemps > 0)
                        {
                            Debug.LogWarning($"Retrying web request: {url} ({remainingAttemps} attemps remaining)");
                            resultOp.Dispose();
                            resultOp = SendWebRequest(requestType, url, downloadHandler, OnSuccess, OnFail, remainingAttemps, timeout, disposeOnCompleted);
                        }
                        else
                        {
                            OnFail?.Invoke(resultOp.webRequest);
                            resultOp.SetAsCompleted(false);
                        }
                    }
                    else
                    {
                        OnFail?.Invoke(resultOp.webRequest);
                        resultOp.SetAsCompleted(false);
                    }
                }

                ongoingWebRequests.Remove(resultOp);
            };

            return(resultOp);
        }