This commit is contained in:
sc authored and Administrator committed 2026-05-08 02:37:51 +08:00
commit 07b73a7185
3 files changed
+41 -16

No files matched your search

+29 -8
View File
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using TouchSocket.Core;
@@ -18,12 +20,25 @@ namespace SunlightAggregationTerminal.Class
public required string Dat { get; set; }
}
public static string GetLocalIpSimple()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip))
{
return ip.ToString();
}
}
return "No network connection";
}
public static string ServerIP = "";
public static string ServerName = "";
public static string ServerPassword = "";
public static string ServerTerminal = "";
public static TcpClient tcpClient = new TcpClient();
public static TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient();
public static void Configuration(string ip, string Name, string Password, string Terminal)
{
@@ -42,7 +57,7 @@ namespace SunlightAggregationTerminal.Class
{
User = ServerName,
Password = ServerPassword,
IP = ServerIP,
IP = GetLocalIpSimple(),
Terminal = ServerTerminal,
Dat = Dat
};
@@ -53,17 +68,19 @@ namespace SunlightAggregationTerminal.Class
{
await tcpClient.SendAsync(JsonSerializer.Serialize(dat));
}
catch (Exception)
catch (Exception ex)
{
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "请求异常", "是");
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", ex.ToString(), "是");
}
}
public static async Task TcpClient(TcpClient tcpClient, string ip)
public static async Task TcpClient(TouchSocket.Sockets.TcpClient tcpClient, string ip)
{
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器
tcpClient.Connected = (client, e) => {
TcpServer.TcpTransmit("Log");
return EasyTask.CompletedTask; };//成功连接到服务器
tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
#region Tcp客户端使用Received异步委托接收数据
@@ -82,8 +99,12 @@ namespace SunlightAggregationTerminal.Class
if (data.TryGetValue("Enterprise", out var val) && val != null) App.GlobalData.Enterprise = val.ToString() ?? "";
if (data.TryGetValue("Department", out val) && val != null) App.GlobalData.Department = val.ToString() ?? "";
if (data.TryGetValue("Groups", out val) && val != null) App.GlobalData.Groups = val.ToString() ?? "";
if (data.TryGetValue("UserId", out val) && val != null) App.GlobalData.UserId = val.ToString() ?? "";
if (data.TryGetValue("UserName", out val) && val != null) App.GlobalData.UserName = val.ToString() ?? "";
if (data.TryGetValue("UserId", out val) && val != null)
{
App.GlobalData.UserId = val.ToString() ?? "";
App.GlobalData.LogNo = true;
}
if (data.TryGetValue("UserData", out var value_UserData))
{
@@ -144,7 +165,7 @@ namespace SunlightAggregationTerminal.Class
.SetRemoteIPHost(ip));
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
}
catch (Exception)
catch (Exception ex)
{
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "不合法的访问地址", "是");
}