Переглянути джерело

6/17更新

2025/6/17 添加对400 error code处理,直接跳转到登录页面
添加对多个服务器检测并自动切换功能
Jees 1 день тому
батько
коміт
6efef013c8

+ 1 - 1
Assets/Resources/MessageBox/MessageBox.prefab

@@ -810,7 +810,7 @@ GameObject:
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0
   m_StaticEditorFlags: 0
-  m_IsActive: 1
+  m_IsActive: 0
 --- !u!4 &2058455808138415937
 Transform:
   m_ObjectHideFlags: 0

+ 1 - 1
Assets/Scripts/EnviromentSetting.cs

@@ -7,7 +7,7 @@ using System.Collections.Generic;
 public static class EnviromentSetting
 {
     // 服务器ip 正式发布需要替换掉ip
-    public static string serverIP = "http://101.34.23.118";
+    public static string serverIP = string.Empty;
     public static string[] serverIPs = { "http://127.0.0.1", "http://101.34.23.118" };
 
     //language.json读取和存放

+ 48 - 1
Assets/Scripts/Functions/WebController.cs

@@ -3,6 +3,7 @@ using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using Newtonsoft.Json;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.SceneManagement;
@@ -87,8 +88,25 @@ public class WebController : MonoBehaviour
             }
             else
             {
-                // 上传成功,获取服务器响应
                 string responseText = request.downloadHandler.text;
+                // 检测是否存在error code
+                var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseText);
+                if (data != null && data.ContainsKey("code") && data.ContainsKey("status"))
+                {
+                    if (data["status"].ToString() == "error")
+                    {
+                        int errorCode = Convert.ToInt32(data["code"]);
+                        if (errorCode == 400)
+                        {
+                            // 如果是400错误,跳转登录场景
+                            Debug.Log("Access token expired or invalid, redirecting to login scene.");
+                            GameData.subScene = null;
+                            SceneManager.LoadScene("Login");
+                            yield break; // 退出协程
+                        }
+                    }
+                }
+                // 上传成功,获取服务器响应
                 callback?.Invoke(responseText);
                 Debug.Log("上传成功!服务器响应: " + responseText);
             }
@@ -98,6 +116,33 @@ public class WebController : MonoBehaviour
     public static IEnumerator ServerAliveCheck()
     {
         string url = "/api/server/alive/";
+        // 尝试从 PlayerPrefs 中获取上次使用的服务器IP
+        if (PlayerPrefs.HasKey("last_serverIP"))
+        {
+            EnviromentSetting.serverIP = PlayerPrefs.GetString("last_serverIP");
+            Debug.Log("使用上次的服务器IP: " + EnviromentSetting.serverIP);
+        }
+
+        // 检测上次使用的服务器IP是否可用
+        if (!string.IsNullOrEmpty(EnviromentSetting.serverIP))
+        {
+            string fullUrl = EnviromentSetting.serverIP + url;
+            using (UnityWebRequest request = UnityWebRequest.Get(fullUrl))
+            {
+                yield return request.SendWebRequest();
+
+                if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
+                {
+                    Debug.Log("上次使用的服务器不可用: " + fullUrl + ": " + request.error);
+                }
+                else
+                {
+                    Debug.Log("上次使用的服务器可用: " + fullUrl);
+                    yield break; // 如果上次使用的服务器可用,直接返回
+                }
+            }
+        }
+        
         int serverIPIndex = 0;
         foreach (string serverIP in EnviromentSetting.serverIPs)
         {
@@ -115,6 +160,7 @@ public class WebController : MonoBehaviour
                 {
                     Debug.Log("服务器可用: " + fullUrl);
                     EnviromentSetting.serverIP = serverIP; // 更新当前使用的服务器IP
+                    PlayerPrefs.SetString("last_serverIP", serverIP); // 保存到PlayerPrefs
                     yield break; // 如果有一个服务器可用,直接返回
                 }
             }
@@ -122,6 +168,7 @@ public class WebController : MonoBehaviour
 
         if (serverIPIndex >= EnviromentSetting.serverIPs.Length)
         {
+            PlayerPrefs.DeleteKey("last_serverIP"); // 清除上次使用的服务器IP
             string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "network_error", EnviromentSetting.languageCode });
             MessageBoxController.ShowMessage(msg, () => Application.Quit());
             Application.Quit();

+ 2 - 2
Assets/Scripts/Login/LoginController.cs

@@ -36,7 +36,7 @@ public class LoginController : MonoBehaviour
         createAdoptCanvas = canvasPlaceholder.transform.Find("Create Or Adopt Canvas").gameObject;
         loginCanvas = canvasPlaceholder.transform.Find("Login Canvas").gameObject;
 
-        StartCoroutine(PingServer());
+        // StartCoroutine(PingServer());
 
         // 判断是否要切换到注册狗的子场景
         if (GameData.subScene == "Login_InitDog")
@@ -47,7 +47,7 @@ public class LoginController : MonoBehaviour
         else
         {
             // 检测是否可以ping到任何一台服务器
-            // StartCoroutine(WebController.ServerAliveCheck());
+            StartCoroutine(WebController.ServerAliveCheck());
             // 启动自动采用Login Token 登录。其他登录方式为手动登录。
             StartCoroutine(LoginTokenRequest());
         }