|
@@ -5,6 +5,7 @@ using UnityEngine.Networking;
|
|
using System.Text;
|
|
using System.Text;
|
|
using GFGGame.Launcher;
|
|
using GFGGame.Launcher;
|
|
using System.Reflection;
|
|
using System.Reflection;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
|
|
namespace GFGGame
|
|
namespace GFGGame
|
|
{
|
|
{
|
|
@@ -13,6 +14,10 @@ namespace GFGGame
|
|
/// </summary>
|
|
/// </summary>
|
|
public class HttpTool : SingletonMonoBase<HttpTool>
|
|
public class HttpTool : SingletonMonoBase<HttpTool>
|
|
{
|
|
{
|
|
|
|
+ //最大尝试次数
|
|
|
|
+ const int TryTimes = 3;
|
|
|
|
+ //存储尝试次数
|
|
|
|
+ public Dictionary<string, int> TryTimesDic = new Dictionary<string, int>();
|
|
|
|
|
|
public void Get(string url, Action<string> callback, bool showWrong = true)
|
|
public void Get(string url, Action<string> callback, bool showWrong = true)
|
|
{
|
|
{
|
|
@@ -27,15 +32,34 @@ namespace GFGGame
|
|
|
|
|
|
public IEnumerator GetRequest(string url, Action<string> callback, bool showWrong)
|
|
public IEnumerator GetRequest(string url, Action<string> callback, bool showWrong)
|
|
{
|
|
{
|
|
- ET.Log.Debug("get url : " + url);
|
|
|
|
|
|
+ var key = url;
|
|
|
|
+ ET.Log.Debug("get url : " + key);
|
|
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
|
|
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
|
|
{
|
|
{
|
|
webRequest.timeout = LauncherConfig.HTTP_GET_TIME_OUT;
|
|
webRequest.timeout = LauncherConfig.HTTP_GET_TIME_OUT;
|
|
|
|
+ TryTimesDic.TryGetValue(key, out var times);
|
|
|
|
+ times++;
|
|
|
|
+ TryTimesDic[key] = times;
|
|
yield return webRequest.SendWebRequest();
|
|
yield return webRequest.SendWebRequest();
|
|
- ResultHandler(webRequest, callback, url, () =>
|
|
|
|
|
|
+ ResultHandler(webRequest, callback, url, () =>
|
|
{
|
|
{
|
|
- Get(url, callback);
|
|
|
|
- }, showWrong);
|
|
|
|
|
|
+ if(times < HttpTool.TryTimes)
|
|
|
|
+ {
|
|
|
|
+ Get(url, callback, showWrong);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ if (showWrong)
|
|
|
|
+ {
|
|
|
|
+ TryTimesDic.Remove(key);
|
|
|
|
+ Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
|
|
|
|
+ .SetRightButton(true, "重试", (object data) =>
|
|
|
|
+ {
|
|
|
|
+ Get(url, callback, showWrong);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -52,8 +76,8 @@ namespace GFGGame
|
|
|
|
|
|
private IEnumerator PostRequest(string url, string body, Action<string> callback, string contentType, bool showWrong)
|
|
private IEnumerator PostRequest(string url, string body, Action<string> callback, string contentType, bool showWrong)
|
|
{
|
|
{
|
|
-
|
|
|
|
- ET.Log.Debug(string.Format("post url:{0} postData:{1}", url, body));
|
|
|
|
|
|
+ var key = url + ":" + body;
|
|
|
|
+ ET.Log.Debug(string.Format($"post {key}", url, body));
|
|
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
{
|
|
{
|
|
byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
|
|
byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
|
|
@@ -62,29 +86,39 @@ namespace GFGGame
|
|
webRequest.timeout = LauncherConfig.HTTP_POST_TIME_OUT;
|
|
webRequest.timeout = LauncherConfig.HTTP_POST_TIME_OUT;
|
|
//http header 的内容
|
|
//http header 的内容
|
|
webRequest.SetRequestHeader("Content-Type", contentType);
|
|
webRequest.SetRequestHeader("Content-Type", contentType);
|
|
- // webRequest.timeout;
|
|
|
|
|
|
+ TryTimesDic.TryGetValue(key, out var times);
|
|
|
|
+ times++;
|
|
|
|
+ TryTimesDic[key] = times;
|
|
yield return webRequest.SendWebRequest();
|
|
yield return webRequest.SendWebRequest();
|
|
- ResultHandler(webRequest, callback, body, () =>
|
|
|
|
|
|
+ ResultHandler(webRequest, callback, key, () =>
|
|
{
|
|
{
|
|
- Post(url, body, callback);
|
|
|
|
- }, showWrong);
|
|
|
|
|
|
+ if (times < HttpTool.TryTimes)
|
|
|
|
+ {
|
|
|
|
+ Post(url, body, callback,contentType, showWrong);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ if (showWrong)
|
|
|
|
+ {
|
|
|
|
+ TryTimesDic.Remove(key);
|
|
|
|
+ Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
|
|
|
|
+ .SetRightButton(true, "重试", (object data) =>
|
|
|
|
+ {
|
|
|
|
+ Post(url, body, callback, contentType, showWrong);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryCall, bool showWrong)
|
|
|
|
|
|
+ public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryFunc)
|
|
{
|
|
{
|
|
string paramCallback = null;
|
|
string paramCallback = null;
|
|
if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
{
|
|
{
|
|
- ET.Log.Error(webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
|
- if(showWrong)
|
|
|
|
- {
|
|
|
|
- Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
|
|
|
|
- .SetRightButton(true, "重试", (object data) =>
|
|
|
|
- {
|
|
|
|
- retryCall();
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ ET.Log.Error(webRequest.error + "\n" + webRequest.downloadHandler.text + "\nby " + tag);
|
|
|
|
+ retryFunc?.Invoke();
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|