fix:调整httpserver脚本请求

This commit is contained in:
bzx
2026-02-10 15:05:42 +08:00
parent cb0a32db52
commit 730e049be5

View File

@@ -4,6 +4,7 @@ using System.Net;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using UnityEngine;
using Valheim;
@@ -42,12 +43,13 @@ public class HttpServer : MonoBehaviour
void Awake()
{
DontDestroyOnLoad(gameObject);
//DontDestroyOnLoad(gameObject)
Task.Run(StartServer);
}
void Start()
{
StartServer();
}
#region HTTP Server
@@ -57,7 +59,7 @@ public class HttpServer : MonoBehaviour
try
{
listener = new HttpListener();
listener.Prefixes.Add(SERVER_URL);
listener.Prefixes.Add($"http://{GetLocalIP()}:12345/");
listener.Start();
isRunning = true;
@@ -74,7 +76,18 @@ public class HttpServer : MonoBehaviour
Debug.LogError("❌ HTTP Server 启动失败:" + e);
}
}
private string GetLocalIP()
{
var host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
return ip.ToString();
}
return "127.0.0.1";
}
private void ListenLoop()
{
while (isRunning && listener.IsListening)