|
@@ -3,6 +3,7 @@ using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
|
+using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.SceneManagement;
|
|
@@ -87,8 +88,25 @@ public class WebController : MonoBehaviour
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- // 上传成功,获取服务器响应
|
|
|
|
string responseText = request.downloadHandler.text;
|
|
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);
|
|
callback?.Invoke(responseText);
|
|
Debug.Log("上传成功!服务器响应: " + responseText);
|
|
Debug.Log("上传成功!服务器响应: " + responseText);
|
|
}
|
|
}
|
|
@@ -98,6 +116,33 @@ public class WebController : MonoBehaviour
|
|
public static IEnumerator ServerAliveCheck()
|
|
public static IEnumerator ServerAliveCheck()
|
|
{
|
|
{
|
|
string url = "/api/server/alive/";
|
|
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;
|
|
int serverIPIndex = 0;
|
|
foreach (string serverIP in EnviromentSetting.serverIPs)
|
|
foreach (string serverIP in EnviromentSetting.serverIPs)
|
|
{
|
|
{
|
|
@@ -115,6 +160,7 @@ public class WebController : MonoBehaviour
|
|
{
|
|
{
|
|
Debug.Log("服务器可用: " + fullUrl);
|
|
Debug.Log("服务器可用: " + fullUrl);
|
|
EnviromentSetting.serverIP = serverIP; // 更新当前使用的服务器IP
|
|
EnviromentSetting.serverIP = serverIP; // 更新当前使用的服务器IP
|
|
|
|
+ PlayerPrefs.SetString("last_serverIP", serverIP); // 保存到PlayerPrefs
|
|
yield break; // 如果有一个服务器可用,直接返回
|
|
yield break; // 如果有一个服务器可用,直接返回
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -122,6 +168,7 @@ public class WebController : MonoBehaviour
|
|
|
|
|
|
if (serverIPIndex >= EnviromentSetting.serverIPs.Length)
|
|
if (serverIPIndex >= EnviromentSetting.serverIPs.Length)
|
|
{
|
|
{
|
|
|
|
+ PlayerPrefs.DeleteKey("last_serverIP"); // 清除上次使用的服务器IP
|
|
string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "network_error", EnviromentSetting.languageCode });
|
|
string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "network_error", EnviromentSetting.languageCode });
|
|
MessageBoxController.ShowMessage(msg, () => Application.Quit());
|
|
MessageBoxController.ShowMessage(msg, () => Application.Quit());
|
|
Application.Quit();
|
|
Application.Quit();
|