HttpMsg.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System;
  8. namespace Engine.Http
  9. {
  10. public delegate void HandlerResponser(HttpResponse responder);
  11. public delegate void HandlerRequestTimeout(HttpRequest request);
  12. /// <summary>http消息</summary>
  13. internal class HttpMsg
  14. {
  15. /// <summary>Http请求消息头</summary>
  16. private static Dictionary<string, string> MsgHeaders = new Dictionary<string, string>() { { "Content-Type", "application/x-www-form-urlencoded" } };
  17. /// <summary>消息对象</summary>
  18. private WWW mHttp3W;
  19. /// <summary>消息请求参数对象</summary>
  20. public HttpRequest mHttpRequest;
  21. /// <summary>是否使用中</summary>
  22. public bool IsUseIng = false;
  23. /// <summary>请求是否完成</summary>
  24. public bool IsRequestComplete = false;
  25. /// <summary>请求进度,例如进度是10%返回值是10</summary>
  26. public int RequestProgress = 0;
  27. /// <summary>响应对象</summary>
  28. public HttpResponse mHttpResponse;
  29. /// <summary>响应时间</summary>
  30. private int mResponseTime = 0;
  31. private bool mIsTimeout = false;
  32. private Action<HttpResponse> httpCallBack;//回调函数 dudu add 2020-06-28
  33. /// <summary>开始下载</summary>
  34. public HttpMsg() { }
  35. /// <summary>发送消息</summary>
  36. public void SendMsg(HttpRequest requestParam, Action<HttpResponse> httpCallBack = null)
  37. {
  38. //使用中
  39. IsUseIng = true;
  40. //保存请求参数对象
  41. mHttpRequest = requestParam;
  42. this.httpCallBack = httpCallBack;
  43. //创建请求对象 创建后请求已经开始发送
  44. if (mHttpRequest.httpMethod == HttpMethod.POST)
  45. {
  46. //byte[] bytes = ObjectToBytes(mHttpRequest.postData);
  47. mHttp3W = new WWW(mHttpRequest.URL, System.Text.UTF8Encoding.UTF8.GetBytes(mHttpRequest.RequestParamContent), MsgHeaders);
  48. }else
  49. {
  50. mHttp3W = new WWW(mHttpRequest.URLAndParamsForGet);
  51. }
  52. CDebug.Log("http网络请求已发送:" + mHttpRequest.URL + "?"+ (mHttpRequest.httpMethod == HttpMethod.POST));
  53. }
  54. public void SendCallBack()
  55. {
  56. if(httpCallBack != null)
  57. {
  58. httpCallBack.Invoke(mHttpResponse);
  59. }
  60. }
  61. public static byte[] ObjectToBytes(object obj) {
  62. using (MemoryStream ms = new MemoryStream()) {
  63. IFormatter formatter = new BinaryFormatter();
  64. formatter.Serialize(ms, obj);
  65. return ms.GetBuffer();
  66. }
  67. }
  68. /// <summary>帧函数,检查请求是否完成</summary>
  69. public void OnUpdate(int nTime)
  70. {
  71. if (mHttp3W == null || IsRequestComplete || mIsTimeout)
  72. { return; }
  73. mResponseTime += nTime;
  74. if (mHttp3W.isDone)
  75. {
  76. //下载完成后的第二帧修改完成 标记
  77. if (RequestProgress == 100)
  78. {
  79. IsRequestComplete = true;
  80. }
  81. RequestProgress = 100;
  82. if (IsRequestComplete)
  83. {
  84. //创建响应对象
  85. mHttpResponse = HttpResponse.OnCreateResponse(mHttp3W.text, mHttpRequest);
  86. }
  87. }
  88. else
  89. {
  90. RequestProgress = (int)(mHttp3W.progress * 100);
  91. if(mResponseTime >= HttpManager.ResponseOverTimeLimit)
  92. {
  93. mResponseTime = 0;
  94. mIsTimeout = true;
  95. /*if (mHttpRequest.requestTimeout != null)//TODO 请求超时
  96. {
  97. mHttpRequest.requestTimeout(mHttpRequest);
  98. }*/
  99. }
  100. }
  101. }
  102. /// <summary>重置请求对象</summary>
  103. public void Reset()
  104. {
  105. IsUseIng = false;
  106. mHttp3W = null;
  107. mHttpRequest.Reset();
  108. mHttpRequest = null;
  109. if(mHttpResponse != null)
  110. mHttpResponse.Reset();
  111. mHttpResponse = null;
  112. RequestProgress = 0;
  113. mResponseTime = 0;
  114. mIsTimeout = false;
  115. IsRequestComplete = false;
  116. }
  117. public float ResponseTime
  118. {
  119. get
  120. {
  121. return mResponseTime;
  122. }
  123. }
  124. public bool IsTimeout
  125. {
  126. get
  127. {
  128. return mIsTimeout;
  129. }
  130. }
  131. }
  132. }