111 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| using UnityEngine.Networking;
 | |
| 
 | |
| namespace MMO
 | |
| {
 | |
|     public class MMOAPIRequestUnit : MonoBehaviour
 | |
|     {
 | |
|         public Action<MMOAPIRequestUnit> DelOver;
 | |
| 
 | |
|         public string RequestURL;
 | |
|         private bool mRequestStarted = false;
 | |
|         private float mTimer = 0;
 | |
|         private const float TIME_OUT_SECONDS = 15;
 | |
|         private Action<int> mErrorCallback;
 | |
| 
 | |
|         private void Update()
 | |
|         {
 | |
|             if (mRequestStarted)
 | |
|             {
 | |
|                 mTimer += Time.unscaledDeltaTime;
 | |
|                 if (mTimer >= TIME_OUT_SECONDS)
 | |
|                 {
 | |
|                     Timeout();
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public void SendRequest<T, U>(T pRequestData, Action<U> pRespondCallback = null, Action<int> pErrorCallback = null) where T : MMORequestBase
 | |
|         {
 | |
| #if UNITY_EDITOR
 | |
|             Debug.Log(string.Format("API {0} request data:{1}", typeof(T).Name, pRequestData.RequestJson()));
 | |
|             Debug.Log(string.Format("API {0} request text:{1}", typeof(T).Name, MMOEncrypt.AESEncrypt(pRequestData.RequestJson())));
 | |
| #endif
 | |
|             byte[] tPostBytes = System.Text.Encoding.Default.GetBytes(MMOEncrypt.AESEncrypt(pRequestData.RequestJson()));
 | |
| 
 | |
|             mErrorCallback = pErrorCallback;
 | |
|             mRequestStarted = true;
 | |
|             mTimer = 0;
 | |
|             RequestURL = pRequestData.RequestURL;
 | |
|             StartCoroutine(PostRequest(pRequestData.RequestURL, tPostBytes, pRespondCallback, pErrorCallback));
 | |
|         }
 | |
| 
 | |
|         IEnumerator PostRequest<U>(string pUrl, byte[] pPostBytes, Action<U> pRespondCallback = null, Action<int> pErrorCallback = null)
 | |
|         {
 | |
|             UnityWebRequest tRequest = new UnityWebRequest(pUrl, "POST");
 | |
| 
 | |
|             tRequest.uploadHandler = new UploadHandlerRaw(pPostBytes);
 | |
|             tRequest.downloadHandler = new DownloadHandlerBuffer();
 | |
|             tRequest.SetRequestHeader("Content-Type", "application/json");
 | |
| 
 | |
|             yield return tRequest.SendWebRequest();
 | |
|             Debug.Log("Status Code: " + tRequest.responseCode);
 | |
| #if UNITY_2020_1_OR_NEWER
 | |
|             if (tRequest.result == UnityWebRequest.Result.ConnectionError || tRequest.result == UnityWebRequest.Result.ProtocolError || tRequest.result == UnityWebRequest.Result.DataProcessingError)
 | |
| #else
 | |
|             if (tRequest.isNetworkError || tRequest.isHttpError)
 | |
| #endif
 | |
|             {
 | |
|                 pErrorCallback?.Invoke((int)tRequest.responseCode);
 | |
|                 Debug.LogError(tRequest.error);
 | |
|             }
 | |
| #if UNITY_2020_1_OR_NEWER
 | |
|             else if (tRequest.result == UnityWebRequest.Result.Success)
 | |
| #else
 | |
|             else
 | |
| #endif
 | |
|             {
 | |
|                 APIRespond<U> tData = JsonUtility.FromJson<APIRespond<U>>(MMOEncrypt.AESDecrypt(tRequest.downloadHandler.text));
 | |
| #if UNITY_EDITOR
 | |
|                 Debug.Log(string.Format("API {0} respond text:{1}", typeof(U).Name, tRequest.downloadHandler.text));
 | |
|                 Debug.Log(string.Format("API {0} respond data:{1}", typeof(U).Name, MMOEncrypt.AESDecrypt(tRequest.downloadHandler.text)));
 | |
| #endif
 | |
|                 if (tData.status == 1)
 | |
|                 {
 | |
|                     pRespondCallback?.Invoke(tData.data);
 | |
|                 }
 | |
|                 else
 | |
|                 {
 | |
|                     pErrorCallback?.Invoke(tData.status);
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             Over();
 | |
|         }
 | |
| 
 | |
|         private void Timeout()
 | |
|         {
 | |
|             StopAllCoroutines();
 | |
|             mErrorCallback?.Invoke(-1);
 | |
|             Over();
 | |
|         }
 | |
| 
 | |
|         private void Over()
 | |
|         {
 | |
|             mRequestStarted = false;
 | |
|             DelOver?.Invoke(this);
 | |
|         }
 | |
| 
 | |
|         [Serializable]
 | |
|         public class APIRespond<U>
 | |
|         {
 | |
|             public int status;
 | |
|             public string error_msg;
 | |
|             public U data;
 | |
|         }
 | |
|     }
 | |
| }
 |