Skip to content

Commit daeddad

Browse files
committed
[增加]1. 增加Swagger的地址列表打印
1 parent 2d22eae commit daeddad

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

GameFrameX.StartUp/AppStartUpByServer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ private async Task StartServer<TMessageDecoderHandler, TMessageEncoderHandler>(L
343343
}
344344
}
345345
});
346+
if (development)
347+
{
348+
var ipList = Net.GetLocalIpList();
349+
foreach (var ip in ipList)
350+
{
351+
LogHelper.DebugConsole($"Swagger UI 可通过 http://{ip}:{Setting.HttpPort}/swagger 访问");
352+
}
353+
}
346354
});
347355
});
348356
}

GameFrameX.Utility/Utility.Net.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Net;
22
using System.Net.NetworkInformation;
3+
using System.Net.Sockets;
34

45
namespace GameFrameX.Utility;
56

@@ -99,4 +100,46 @@ public static bool PortIsAvailable(int port)
99100

100101
return isAvailable;
101102
}
103+
104+
/// <summary>
105+
/// 获取本地IP地址列表
106+
/// </summary>
107+
/// <param name="addressFamily">IP地址类型,默认为IPv4</param>
108+
/// <returns>本地IP地址列表</returns>
109+
public static List<string> GetLocalIpList(AddressFamily addressFamily = AddressFamily.InterNetwork)
110+
{
111+
var ipList = new List<string>();
112+
try
113+
{
114+
// 获取本地计算机的网络接口信息
115+
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
116+
foreach (var network in networkInterfaces)
117+
{
118+
// 排除非活动状态和环回接口
119+
if (network.OperationalStatus != OperationalStatus.Up ||
120+
network.NetworkInterfaceType == NetworkInterfaceType.Loopback)
121+
{
122+
continue;
123+
}
124+
125+
// 获取网络接口的IP属性
126+
var properties = network.GetIPProperties();
127+
foreach (var address in properties.UnicastAddresses)
128+
{
129+
// 根据指定的地址类型筛选IP
130+
if (address.Address.AddressFamily == addressFamily)
131+
{
132+
ipList.Add(address.Address.ToString());
133+
}
134+
}
135+
}
136+
}
137+
catch (Exception)
138+
{
139+
// 发生异常时返回空列表
140+
return new List<string>();
141+
}
142+
143+
return ipList;
144+
}
102145
}

0 commit comments

Comments
 (0)