WebRequestServerBase.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 一个WebRequest发送系统的基类
  6. /// </summary>
  7. public abstract class WebRequestServerBase : SystemBase {
  8. List<WebRequestBase> webRequestList = new List<WebRequestBase>();
  9. protected void AddWebRequest(WebRequestBase webRequest) {
  10. webRequestList.Add(webRequest);
  11. CheckQueue();
  12. }
  13. void CheckQueue() {
  14. if (webRequestList.Count == 0) {
  15. return;
  16. }
  17. StartCoroutine(SendRequest());
  18. }
  19. IEnumerator SendRequest() {
  20. ///加yield return null目的是将Web请求延迟到下一帧开始,避免web回复委托还没添加,web已经回复,导致错过信息
  21. yield return null;
  22. WebRequestBase webRequest = webRequestList[0];
  23. webRequestList.RemoveAt(0);
  24. yield return StartCoroutine(webRequest.SendRequest());
  25. CheckQueue();
  26. }
  27. public override void SystemStop() {
  28. base.SystemStop();
  29. StopAllCoroutines();
  30. webRequestList.Clear();
  31. }
  32. }