using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class WebRequestResult
{
/// <summary>
/// 0为请求成功 1位请求失败
/// </summary>
public int err = 0;
public string errMsg = "";
public DownloadHandler result ;
}
public class WebRequestManager : MonoBehaviour {
// Use this for initialization
private static WebRequestManager instance;
public delegate void WebRequestResultListener(WebRequestResult request);
public static Dictionary<string, WebRequestResultListener> msgListeners = new Dictionary<string, WebRequestResultListener>();
public static WebRequestManager Instance
{
get
{
if (instance != null)
{
return instance;
}
return instance=new GameObject("WebRequestManager").AddComponent< WebRequestManager>();
}
}
public void Post( WWWForm form,string url, string msgName,WebRequestResultListener webRequestResultListener)
{
StartCoroutine(WebPost(msgName,form, url, webRequestResultListener));
}
IEnumerator WebPost(string msgName,WWWForm form,string url, WebRequestResultListener webRequestResultListener)
{
WebRequestResult postResult=new WebRequestResult();
//请求链接,并将form对象发送到远程服务器
UnityWebRequest webRequest = UnityWebRequest.Post(url, form);
AddMsgListener(msgName,webRequestResultListener);
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
postResult.err = 1;
postResult.errMsg = webRequest.error;
}
else
{
postResult.err = 0;
postResult.result = webRequest.downloadHandler;
}
FireMsg(msgName, postResult);
RemoveMsgListener(msgName,webRequestResultListener);
}
public void Get<T>(string msgName, string url, WebRequestResultListener webRequestResultListener) where T : DownloadHandler, new()
{
StartCoroutine(WebGet<T>(msgName, url,webRequestResultListener));
}
public IEnumerator WebGet<T>(string msgName, string url, WebRequestResultListener webRequestResultListener)where T : DownloadHandler, new()
{
WebRequestResult getResult = new WebRequestResult();
UnityWebRequest webRequest = UnityWebRequest.Get(url);
AddMsgListener(msgName, webRequestResultListener);
T handler = new T();
webRequest.downloadHandler = handler;
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
getResult.err = 1;
getResult.errMsg = webRequest.error;
}
else //请求成功
{
getResult.err = 0;
getResult.result = handler;
}
FireMsg(msgName, getResult);
RemoveMsgListener(msgName, webRequestResultListener);
}
public void Get(string msgName, string url, WebRequestResultListener webRequestResultListener)
{
StartCoroutine(WebGet(msgName, url, webRequestResultListener));
}
public IEnumerator WebGet(string msgName, string url, WebRequestResultListener webRequestResultListener)
{
WebRequestResult getResult = new WebRequestResult();
UnityWebRequest webRequest = UnityWebRequest.Get(url);
AddMsgListener(msgName, webRequestResultListener);
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
getResult.err = 1;
getResult.errMsg = webRequest.error;
}
else //请求成功
{
getResult.err = 0;
getResult.result = webRequest.downloadHandler;
}
FireMsg(msgName, getResult);
RemoveMsgListener(msgName, webRequestResultListener);
}
public void AddMsgListener(string msgName, WebRequestResultListener listener)
{
if (msgListeners.ContainsKey(msgName))
{
msgListeners[msgName] += listener;
}
else
{
msgListeners[msgName] = listener;
}
}
public void RemoveMsgListener(string msgName, WebRequestResultListener listener)
{
if (msgListeners.ContainsKey(msgName))
{
msgListeners[msgName] -= listener;
if (msgListeners[msgName] == null)
msgListeners.Remove(msgName);
}
}
private void FireMsg(string msgName, WebRequestResult msgBase)
{
if (msgListeners.ContainsKey(msgName))
{
msgListeners[msgName](msgBase);
}
}
}
使用方式:
WebRequestManager.Instance.Get("GetAdr", _url, (request =>
{
if (request.err == 0)
{
print("成功");
}
else
{
print(request.errMsg + "--请求失败 重新请求");
}
}));
|