Example #1
0
 public IEnumerator waitTradeQuery1(RequestBody inBody, ResponseBody outBody)
 {
     inBody.method = methods[3];
     yield return(StartCoroutine(waitEdo(inBody, outBody)));
 }
Example #2
0
        protected IEnumerator waitEdo(RequestBody inBody, ResponseBody outBody)
        {
            serverUrl = "http://127.0.0.1:8000/api";

            inBody.appId        = AppId;
            inBody.format       = "JSON";
            inBody.charset      = "utf-8";
            inBody.timestamp    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            inBody.version      = "1.0.0";
            inBody.appAuthToken = token;

            inBody.signCode = signCodeIn(inBody);

            //var apiUrl = string.Format("{0}{1}?token={2}", serverUrl, inBody.method, inBody.appAuthToken);
            var apiUrl = string.Format("{0}{1}?token={2}", serverUrl, "/toodo/edo", inBody.appAuthToken);

            var headers = new Dictionary <string, string>
            {
                { "Content-Type", "application/json" },
                { "Accept", "application/json" }
            };
            var json     = JsonConvert.SerializeObject(inBody);
            var postData = Encoding.UTF8.GetBytes(json);
            var www      = new WWW(apiUrl, postData, headers);


            var startTime = Time.time;
            var isTimeOut = false;

            while (true)
            {
                if (www.isDone)
                {
                    break;
                }
                if (!string.IsNullOrEmpty(www.error))
                {
                    break;
                }
                if (Time.time - startTime > 3.14159f)   //! 超时
                {
                    isTimeOut = true;
                    break;
                }

                yield return(new WaitForEndOfFrame());
            }

            if (isTimeOut)
            {
                //! 超时
                outBody.code    = 10000;
                outBody.msg     = "客户端错误";
                outBody.subCode = "103";
                outBody.subMsg  = "请求超时";
            }
            else
            {
                var error = www.error;

                if (string.IsNullOrEmpty(error))
                {
                    Debug.Log(www.text);
                    //! 成功
                    try {
                        var result = JsonConvert.DeserializeObject <ResponseBody>(www.text);
                        outBody.code       = result.code;
                        outBody.msg        = result.msg;
                        outBody.subCode    = result.subCode;
                        outBody.subMsg     = result.subMsg;
                        outBody.timestamp  = result.timestamp;
                        outBody.sign       = result.sign;
                        outBody.bizContent = result.bizContent;
                        outBody.token      = result.token;

                        var md5 = signCodeOut(outBody);
                        if (md5 != outBody.sign)
                        {
                            outBody.code    = 10000;
                            outBody.msg     = "客户端错误";
                            outBody.subCode = "102";
                            outBody.subMsg  = "返回结果签名出错";
                        }
                    } catch (Exception exception) {
                        Debug.Log(exception);

                        outBody.code    = 10000;
                        outBody.msg     = "客户端错误";
                        outBody.subCode = "101";
                        outBody.subMsg  = "返回结果异常";
                    }
                }
                else
                {
                    Debug.Log(error);
                    if (www.isDone)
                    {
                        File.WriteAllBytes("error.html", www.bytes);
                    }

                    //!出错
                    outBody.code    = 10000;
                    outBody.msg     = "客户端错误";
                    outBody.subCode = "104";
                    outBody.subMsg  = "请求返回错误";
                }

                www.Dispose();
            }

            Debug.LogFormat("-e code:{0} msg:{1} subCode:{2} subMsg:{3}", outBody.code, outBody.msg, outBody.subCode, outBody.subMsg);
            if (outBody.code == 0)
            {
                if (!string.IsNullOrEmpty(outBody.token))
                {
                    token = outBody.token;
                }
            }
        }
Example #3
0
 public IEnumerator waitEdoGameRun(RequestBody inBody, ResponseBody outBody)
 {
     inBody.method = methods[7];
     yield return(StartCoroutine(waitEdo(inBody, outBody)));
 }