WebRequestMgr.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. namespace SC.XR.Unity.Module_PlatformAccount
  10. {
  11. /// <summary>
  12. /// 请求数据
  13. /// </summary>
  14. public struct RequestData
  15. {
  16. public IEnumerator enumerator;
  17. public Task task;
  18. /// <summary>
  19. /// 开始请求时触发的事件
  20. /// </summary>
  21. public Action requestStartEvent;
  22. /// <summary>
  23. /// 请求中触发的事件(Update)
  24. /// </summary>
  25. public Action requestingEvent;
  26. /// <summary>
  27. /// 结束请求时触发的事件
  28. /// </summary>
  29. public Action requestEndEvent;
  30. }
  31. public abstract class WebRequestMgr : SingletonMono<WebRequestManager>
  32. {
  33. /// <summary>
  34. /// 当前正在请求的数据
  35. /// </summary>
  36. private RequestData m_CurrentRequestData;
  37. protected virtual async void Update()
  38. {
  39. // 开始队列请求
  40. if (RequsetQueue.Count > 0)
  41. {
  42. // 队列请求中。。。
  43. m_IsRequestingQueue = true;
  44. if (!m_IsRequesting)
  45. {
  46. // 当前请求中。。。
  47. m_IsRequesting = true;
  48. // 所有请求开始触发的事件
  49. RequestStartEvent?.Invoke();
  50. // 当前请求
  51. m_CurrentRequestData = RequsetQueue.Dequeue();
  52. // 当前请求开始触发的事件
  53. m_CurrentRequestData.requestStartEvent?.Invoke();
  54. // 当前请求协程开始
  55. if (m_CurrentRequestData.enumerator!=null)
  56. {
  57. StartCoroutine(m_CurrentRequestData.enumerator);
  58. }
  59. else if (m_CurrentRequestData.task != null)
  60. {
  61. await m_CurrentRequestData.task;
  62. }
  63. }
  64. }
  65. // 队列请求中。。。
  66. else if (m_IsRequesting)
  67. {
  68. // 所有请求中触发的事件
  69. RequestingEvent?.Invoke();
  70. // 当前请求中触发的事件
  71. m_CurrentRequestData.requestingEvent?.Invoke();
  72. }
  73. // 结束队列请求
  74. else if (m_IsRequestingQueue && !m_IsRequesting && m_RequestQueue.Count <= 0)
  75. {
  76. // 队列请求结束
  77. m_IsRequestingQueue = false;
  78. // 所有请求结束触发的事件
  79. RequestEndEvent?.Invoke();
  80. // 当前请求结束触发的事件
  81. m_CurrentRequestData.requestEndEvent?.Invoke();
  82. }
  83. }
  84. /// <summary>
  85. /// 是否正在请求
  86. /// </summary>
  87. private bool m_IsRequesting;
  88. public bool IsRequesting { get { return m_IsRequesting; } set { m_IsRequesting = value; } }
  89. /// <summary>
  90. /// 是否正在队列请求
  91. /// </summary>
  92. private bool m_IsRequestingQueue;
  93. public bool IsRequestingQueue { get { return m_IsRequestingQueue; } set { m_IsRequestingQueue = value; } }
  94. /// <summary>
  95. /// 请求队列
  96. /// </summary>
  97. private Queue<RequestData> m_RequestQueue;
  98. public Queue<RequestData> RequsetQueue
  99. {
  100. get
  101. {
  102. if (m_RequestQueue == null)
  103. {
  104. m_RequestQueue = new Queue<RequestData>();
  105. }
  106. return m_RequestQueue;
  107. }
  108. }
  109. /// <summary>
  110. /// 开始队列请求时触发的事件
  111. /// </summary>
  112. public Action RequestStartEvent;
  113. /// <summary>
  114. /// 队列请求中触发的事件(Update)
  115. /// </summary>
  116. public Action RequestingEvent;
  117. /// <summary>
  118. /// 结束队列请求时触发的事件
  119. /// </summary>
  120. public Action RequestEndEvent;
  121. /// <summary>
  122. /// 准备发送UnityWebRequest之前触发的事件
  123. /// </summary>
  124. public Action<string, UnityWebRequest> PreSendWebRequestEvent;
  125. /// <summary>
  126. /// 发送完成UnityWebRequset之后触发的回调事件
  127. /// </summary>
  128. public Action<string, UnityWebRequest> WebRequestCallBackEvent;
  129. /// <summary>
  130. /// 通用数据Post请求接口
  131. /// </summary>
  132. public abstract void PostData(string url, string headerJson, string formJson, Action<string> callback = null,Action<string> error = null);
  133. public abstract void PostData(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
  134. /// <summary>
  135. /// 图像数据Post请求接口
  136. /// </summary>
  137. public abstract void PostImageData(string url, Texture2D userIcon, HttpClient client, string channel, string filename);
  138. /// <summary>
  139. /// 通用数据Get请求接口
  140. /// </summary>
  141. public abstract void GetData(string url, string headerJson, string formJson, Action<string> callback = null,Action<string> error = null);
  142. public abstract void GetData(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
  143. /// <summary>
  144. /// Get请求队列
  145. /// </summary>
  146. public abstract IEnumerator GetsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
  147. /// <summary>
  148. /// Post请求队列
  149. /// </summary>
  150. public abstract IEnumerator PostsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
  151. }
  152. }