123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System;
- namespace Engine.Http
- {
- public delegate void HandlerResponser(HttpResponse responder);
- public delegate void HandlerRequestTimeout(HttpRequest request);
- /// <summary>http消息</summary>
- internal class HttpMsg
- {
- /// <summary>Http请求消息头</summary>
- private static Dictionary<string, string> MsgHeaders = new Dictionary<string, string>() { { "Content-Type", "application/x-www-form-urlencoded" } };
- /// <summary>消息对象</summary>
- private WWW mHttp3W;
- /// <summary>消息请求参数对象</summary>
- public HttpRequest mHttpRequest;
- /// <summary>是否使用中</summary>
- public bool IsUseIng = false;
- /// <summary>请求是否完成</summary>
- public bool IsRequestComplete = false;
- /// <summary>请求进度,例如进度是10%返回值是10</summary>
- public int RequestProgress = 0;
- /// <summary>响应对象</summary>
- public HttpResponse mHttpResponse;
- /// <summary>响应时间</summary>
- private int mResponseTime = 0;
- private bool mIsTimeout = false;
- private Action<HttpResponse> httpCallBack;//回调函数 dudu add 2020-06-28
- /// <summary>开始下载</summary>
- public HttpMsg() { }
- /// <summary>发送消息</summary>
- public void SendMsg(HttpRequest requestParam, Action<HttpResponse> httpCallBack = null)
- {
- //使用中
- IsUseIng = true;
- //保存请求参数对象
- mHttpRequest = requestParam;
- this.httpCallBack = httpCallBack;
- //创建请求对象 创建后请求已经开始发送
- if (mHttpRequest.httpMethod == HttpMethod.POST)
- {
- //byte[] bytes = ObjectToBytes(mHttpRequest.postData);
- mHttp3W = new WWW(mHttpRequest.URL, System.Text.UTF8Encoding.UTF8.GetBytes(mHttpRequest.RequestParamContent), MsgHeaders);
- }else
- {
- mHttp3W = new WWW(mHttpRequest.URLAndParamsForGet);
- }
- CDebug.Log("http网络请求已发送:" + mHttpRequest.URL + "?"+ (mHttpRequest.httpMethod == HttpMethod.POST));
- }
- public void SendCallBack()
- {
- if(httpCallBack != null)
- {
- httpCallBack.Invoke(mHttpResponse);
- }
- }
- public static byte[] ObjectToBytes(object obj) {
- using (MemoryStream ms = new MemoryStream()) {
- IFormatter formatter = new BinaryFormatter();
- formatter.Serialize(ms, obj);
- return ms.GetBuffer();
- }
- }
-
- /// <summary>帧函数,检查请求是否完成</summary>
- public void OnUpdate(int nTime)
- {
- if (mHttp3W == null || IsRequestComplete || mIsTimeout)
- { return; }
-
- mResponseTime += nTime;
- if (mHttp3W.isDone)
- {
- //下载完成后的第二帧修改完成 标记
- if (RequestProgress == 100)
- {
- IsRequestComplete = true;
- }
- RequestProgress = 100;
- if (IsRequestComplete)
- {
- //创建响应对象
- mHttpResponse = HttpResponse.OnCreateResponse(mHttp3W.text, mHttpRequest);
- }
- }
- else
- {
- RequestProgress = (int)(mHttp3W.progress * 100);
- if(mResponseTime >= HttpManager.ResponseOverTimeLimit)
- {
- mResponseTime = 0;
- mIsTimeout = true;
- /*if (mHttpRequest.requestTimeout != null)//TODO 请求超时
- {
- mHttpRequest.requestTimeout(mHttpRequest);
- }*/
- }
- }
- }
- /// <summary>重置请求对象</summary>
- public void Reset()
- {
- IsUseIng = false;
- mHttp3W = null;
- mHttpRequest.Reset();
- mHttpRequest = null;
- if(mHttpResponse != null)
- mHttpResponse.Reset();
- mHttpResponse = null;
- RequestProgress = 0;
- mResponseTime = 0;
- mIsTimeout = false;
- IsRequestComplete = false;
- }
- public float ResponseTime
- {
- get
- {
- return mResponseTime;
- }
- }
- public bool IsTimeout
- {
- get
- {
- return mIsTimeout;
- }
- }
- }
- }
|