fix: 服务端使用本地IP

This commit is contained in:
2025-07-21 19:19:29 +08:00
parent e7c8d3fb9e
commit 5da9d9f4e2
6 changed files with 50 additions and 23 deletions

View File

@@ -0,0 +1,20 @@
using System.Net;
using System.Net.Sockets;
public static class Global {
public static readonly string localhost = "172.31.2.228";
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
}

View File

@@ -148,10 +148,10 @@ public class VideoStreamController : ControllerBase
FrameWidth = _videoStreamService.FrameWidth,
FrameHeight = _videoStreamService.FrameHeight,
Format = "MJPEG",
HtmlUrl = $"http://localhost:{_videoStreamService.ServerPort}/video-feed.html",
MjpegUrl = $"http://localhost:{_videoStreamService.ServerPort}/video-stream",
SnapshotUrl = $"http://localhost:{_videoStreamService.ServerPort}/snapshot",
UsbCameraUrl = $"http://localhost:{_videoStreamService.ServerPort}/usb-camera"
HtmlUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/video-feed.html",
MjpegUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/video-stream",
SnapshotUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/snapshot",
UsbCameraUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/usb-camera"
};
return TypedResults.Ok(result);
}
@@ -267,7 +267,7 @@ public class VideoStreamController : ControllerBase
using (var httpClient = new HttpClient())
{
httpClient.Timeout = TimeSpan.FromSeconds(2); // 设置较短的超时时间
var response = await httpClient.GetAsync($"http://localhost:{_videoStreamService.ServerPort}/");
var response = await httpClient.GetAsync($"http://{Global.localhost}:{_videoStreamService.ServerPort}/");
// 只要能连接上就认为成功,不管返回状态
isConnected = response.IsSuccessStatusCode;

View File

@@ -311,7 +311,7 @@ public class HttpVideoStreamService : BackgroundService
// 创建 HTTP 监听器
_httpListener = new HttpListener();
_httpListener.Prefixes.Add($"http://localhost:{_serverPort}/");
_httpListener.Prefixes.Add($"http://{Global.localhost}:{_serverPort}/");
_httpListener.Start();
logger.Info("HTTP 视频流服务已启动,监听端口: {Port}", _serverPort);