123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Threading.Tasks;
- using UnityEngine;
- using UnityEngine.Networking;
- namespace SC.XR.Unity.Module_PlatformAccount
- {
- /// <summary>
- /// 请求数据
- /// </summary>
- public struct RequestData
- {
- public IEnumerator enumerator;
- public Task task;
- /// <summary>
- /// 开始请求时触发的事件
- /// </summary>
- public Action requestStartEvent;
- /// <summary>
- /// 请求中触发的事件(Update)
- /// </summary>
- public Action requestingEvent;
- /// <summary>
- /// 结束请求时触发的事件
- /// </summary>
- public Action requestEndEvent;
- }
- public abstract class WebRequestMgr : SingletonMono<WebRequestManager>
- {
- /// <summary>
- /// 当前正在请求的数据
- /// </summary>
- private RequestData m_CurrentRequestData;
- protected virtual async void Update()
- {
- // 开始队列请求
- if (RequsetQueue.Count > 0)
- {
- // 队列请求中。。。
- m_IsRequestingQueue = true;
- if (!m_IsRequesting)
- {
- // 当前请求中。。。
- m_IsRequesting = true;
- // 所有请求开始触发的事件
- RequestStartEvent?.Invoke();
-
- // 当前请求
- m_CurrentRequestData = RequsetQueue.Dequeue();
- // 当前请求开始触发的事件
- m_CurrentRequestData.requestStartEvent?.Invoke();
- // 当前请求协程开始
- if (m_CurrentRequestData.enumerator!=null)
- {
- StartCoroutine(m_CurrentRequestData.enumerator);
- }
- else if (m_CurrentRequestData.task != null)
- {
- await m_CurrentRequestData.task;
- }
- }
- }
- // 队列请求中。。。
- else if (m_IsRequesting)
- {
- // 所有请求中触发的事件
- RequestingEvent?.Invoke();
- // 当前请求中触发的事件
- m_CurrentRequestData.requestingEvent?.Invoke();
- }
- // 结束队列请求
- else if (m_IsRequestingQueue && !m_IsRequesting && m_RequestQueue.Count <= 0)
- {
- // 队列请求结束
- m_IsRequestingQueue = false;
- // 所有请求结束触发的事件
- RequestEndEvent?.Invoke();
- // 当前请求结束触发的事件
- m_CurrentRequestData.requestEndEvent?.Invoke();
- }
- }
- /// <summary>
- /// 是否正在请求
- /// </summary>
- private bool m_IsRequesting;
- public bool IsRequesting { get { return m_IsRequesting; } set { m_IsRequesting = value; } }
- /// <summary>
- /// 是否正在队列请求
- /// </summary>
- private bool m_IsRequestingQueue;
- public bool IsRequestingQueue { get { return m_IsRequestingQueue; } set { m_IsRequestingQueue = value; } }
- /// <summary>
- /// 请求队列
- /// </summary>
- private Queue<RequestData> m_RequestQueue;
- public Queue<RequestData> RequsetQueue
- {
- get
- {
- if (m_RequestQueue == null)
- {
- m_RequestQueue = new Queue<RequestData>();
- }
- return m_RequestQueue;
- }
- }
- /// <summary>
- /// 开始队列请求时触发的事件
- /// </summary>
- public Action RequestStartEvent;
- /// <summary>
- /// 队列请求中触发的事件(Update)
- /// </summary>
- public Action RequestingEvent;
- /// <summary>
- /// 结束队列请求时触发的事件
- /// </summary>
- public Action RequestEndEvent;
- /// <summary>
- /// 准备发送UnityWebRequest之前触发的事件
- /// </summary>
- public Action<string, UnityWebRequest> PreSendWebRequestEvent;
- /// <summary>
- /// 发送完成UnityWebRequset之后触发的回调事件
- /// </summary>
- public Action<string, UnityWebRequest> WebRequestCallBackEvent;
- /// <summary>
- /// 通用数据Post请求接口
- /// </summary>
- public abstract void PostData(string url, string headerJson, string formJson, Action<string> callback = null,Action<string> error = null);
- public abstract void PostData(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
- /// <summary>
- /// 图像数据Post请求接口
- /// </summary>
- public abstract void PostImageData(string url, Texture2D userIcon, HttpClient client, string channel, string filename);
- /// <summary>
- /// 通用数据Get请求接口
- /// </summary>
- public abstract void GetData(string url, string headerJson, string formJson, Action<string> callback = null,Action<string> error = null);
- public abstract void GetData(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
- /// <summary>
- /// Get请求队列
- /// </summary>
- public abstract IEnumerator GetsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
- /// <summary>
- /// Post请求队列
- /// </summary>
- public abstract IEnumerator PostsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null);
- }
- }
|