Browse Source

Http添加尝试次数

guodong 3 years ago
parent
commit
05661ad22f

+ 0 - 1
GameClient/Assets/Game/Launcher/GameLauncher.cs

@@ -30,7 +30,6 @@ public class GameLauncher : MonoBehaviour
     // Start is called before the first frame update
     // Start is called before the first frame update
     void Start()
     void Start()
     {
     {
-        LogServerHelperHttp.SendNodeLog((int)LogNode.OnStart);
         LauncherConfig.Init();
         LauncherConfig.Init();
         Debug.LogFormat("Application.version {0}", Application.version);
         Debug.LogFormat("Application.version {0}", Application.version);
         FGUILauncher.Init();
         FGUILauncher.Init();

+ 4 - 6
GameClient/Assets/Game/Launcher/LauncherConfig.cs

@@ -20,15 +20,14 @@ namespace GFGGame
         //平台id
         //平台id
         public static string cfgUrl = "http://39.99.144.134/res/platform/{cfgName}.json";
         public static string cfgUrl = "http://39.99.144.134/res/platform/{cfgName}.json";
         public static int platformId = 1;
         public static int platformId = 1;
-        public static string logKey = "1";
-        public static string logApiUrl = "http://10.108.64.127:10004/report";
+        public static string logKey;
+        public static string logApiUrl;
 
 
         //public static string CDN_ROOT = "http://10.108.64.58/";
         //public static string CDN_ROOT = "http://10.108.64.58/";
         public static bool ILRuntimeMode;
         public static bool ILRuntimeMode;
 
 
         public static void Init()
         public static void Init()
         {
         {
-            Debug.Log($"identifier: {Application.identifier}");
 #if PT_DEV
 #if PT_DEV
             //外网dev版本
             //外网dev版本
             netType = EnumNetType.DEV;
             netType = EnumNetType.DEV;
@@ -41,8 +40,6 @@ namespace GFGGame
             netType = EnumNetType.LOCAL;
             netType = EnumNetType.LOCAL;
             cfgName = "cfg_local";
             cfgName = "cfg_local";
 #endif
 #endif
-
-            //Debug.Log($"loginApiUrl {loginApiUrl} netType {netType}");
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -65,7 +62,8 @@ namespace GFGGame
             loginApiUrl = result.loginApiUrl;
             loginApiUrl = result.loginApiUrl;
             logApiUrl = result.logApiUrl;
             logApiUrl = result.logApiUrl;
             logKey = result.logKey;
             logKey = result.logKey;
-            Debug.Log($"ptcfg \n{data}");
+            ET.Log.Debug($"ptcfg \n{data}");
+            LogServerHelperHttp.SendNodeLog((int)LogNode.OnStart);
             LauncherView.Instance.SetDesc("获取版本信息...");
             LauncherView.Instance.SetDesc("获取版本信息...");
             VersionController.Instance.Init();
             VersionController.Instance.Init();
         }
         }

+ 54 - 20
GameClient/Assets/Game/Launcher/Net/Http/HttpTool.cs

@@ -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
             {
             {