逻辑结构图:
TestHttpCallBackArgs http回调参数
1 using System; 2 3 /// <summary> 4 /// http回调参数 5 /// </summary> 6 public class TestHttpCallBackArgs : EventArgs 7 { 8 /// <summary> 9 /// 是否有错 10 /// </summary> 11 public bool HasError; 12 /// <summary> 13 /// 错误值 14 /// </summary> 15 public string ErrorValue; 16 /// <summary> 17 /// 值 18 /// </summary> 19 public string Value; 20 /// <summary> 21 /// 数据 22 /// </summary> 23 public byte[] Data; 24 25 public void Init() 26 { 27 HasError = false; 28 ErrorValue = string.Empty; 29 Value = string.Empty; 30 Data = null; 31 } 32 }
TestHttpRoutine http访问器
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.Networking; 5 6 public delegate void HttpSendDataCallBack(TestHttpCallBackArgs callBackArgs); 7 8 /// <summary> 9 /// http访问器 10 /// </summary> 11 public class TestHttpRoutine 12 { 13 private HttpSendDataCallBack m_SendCallBack; 14 private readonly TestHttpCallBackArgs m_CallBackArgs; 15 private bool m_IsBusy; 16 /// <summary> 17 /// 是否获取Data数据 18 /// </summary> 19 private bool m_IsGetData; 20 21 public TestHttpRoutine() 22 { 23 m_CallBackArgs = TestGameEntry.PoolMgr.Class_Dequeue<TestHttpCallBackArgs>(); 24 m_CallBackArgs.Init(); 25 } 26 27 /// <summary> 28 /// 发送数据 29 /// </summary> 30 /// <param name="url"></param> 31 /// <param name="sendCallBack"></param> 32 /// <param name="isPost"></param> 33 /// <param name="isGetData"></param> 34 /// <param name="data"></param> 35 public void SendData(string url, HttpSendDataCallBack sendCallBack, bool isPost = false, bool isGetData = false, Dictionary<object, object> data = null) 36 { 37 if (m_IsBusy) return; 38 m_IsBusy = true; 39 m_IsGetData = isGetData; 40 m_SendCallBack = sendCallBack; 41 string json = string.Empty; 42 if (isPost) 43 { 44 json = LitJson.JsonMapper.ToJson(data); 45 PostUrl(url, json); 46 } 47 else 48 { 49 GetUrl(url); 50 } 51 if (!m_IsGetData) 52 { 53 Debug.Log("<color=#ffa200>发送消息:</color><color=#FFFB80>" + url + "</color>"); 54 if (json != string.Empty) 55 { 56 Debug.Log("<color=#ffdeb3>==>>" + json + "</color>"); 57 } 58 } 59 } 60 61 /// <summary> 62 /// Get请求 63 /// </summary> 64 /// <param name="url"></param> 65 private void GetUrl(string url) 66 { 67 UnityWebRequest webRequest = UnityWebRequest.Get(url); 68 TestGameEntry.Instance.StartCoroutine(Request(webRequest)); 69 } 70 71 /// <summary> 72 /// post请求 73 /// </summary> 74 /// <param name="url"></param> 75 /// <param name="json"></param> 76 private void PostUrl(string url, string json) 77 { 78 WWWForm form = new WWWForm(); 79 form.AddField("json", json); 80 UnityWebRequest webRequest = UnityWebRequest.Post(url, form); 81 TestGameEntry.Instance.StartCoroutine(Request(webRequest)); 82 } 83 84 private IEnumerator Request(UnityWebRequest webRequest) 85 { 86 yield return webRequest.SendWebRequest(); 87 m_IsBusy = false; 88 if (webRequest.result != UnityWebRequest.Result.Success) 89 { 90 m_CallBackArgs.HasError = true; 91 m_CallBackArgs.ErrorValue = webRequest.error; 92 m_SendCallBack?.Invoke(m_CallBackArgs); 93 if (!m_IsGetData) 94 { 95 Debug.Log("<color=#00eaff>接收消息:</color><color=#00ff9c>" + webRequest.url + "</color>"); 96 Debug.Log("<color=#c5e1dc>==>>" + JsonUtility.ToJson(m_CallBackArgs) + "</color>"); 97 } 98 } 99 else 100 { 101 m_CallBackArgs.HasError = false; 102 m_CallBackArgs.Value = webRequest.downloadHandler.text; 103 //webRequest.downloadHandler.data 数据是字节数组,序列化也序列化不出来内容 非GetData打印日志,并在data赋值之前打印 104 if (!m_IsGetData) 105 { 106 Debug.Log("<color=#00eaff>接收消息:</color><color=#00ff9c>" + webRequest.url + "</color>"); 107 Debug.Log("<color=#c5e1dc>==>>" + JsonUtility.ToJson(m_CallBackArgs) + "</color>"); 108 } 109 m_CallBackArgs.Data = webRequest.downloadHandler.data; 110 m_SendCallBack?.Invoke(m_CallBackArgs); 111 } 112 TestGameEntry.PoolMgr.Class_Equeue(m_CallBackArgs); 113 webRequest.Dispose(); 114 webRequest = null; 115 TestGameEntry.PoolMgr.Class_Equeue(this); 116 } 117 }