using System; using System.Collections.Generic; using System.Text; using System.Text.Json; using TouchSocket.Core; using TouchSocket.Sockets; namespace SunlightAggregationTerminal.Class { public class TcpServer { public class Person { public required string User { get; set; } public required string Password { get; set; } public required string IP { get; set; } public required string Terminal { get; set; } public required string Dat { get; set; } } public static string ServerIP = ""; public static string ServerName = ""; public static string ServerPassword = ""; public static string ServerTerminal = ""; public static TcpClient tcpClient = new TcpClient(); public static void Configuration(string ip, string Name, string Password, string Terminal) { ServerIP = ip; ServerName = Name; ServerPassword = Password; ServerTerminal = Terminal; _ = TcpClient(tcpClient, ServerIP); } public static async void TcpTransmit(string Dat) { var dat = new Person() { User = ServerName, Password = ServerPassword, IP = ServerIP, Terminal = ServerTerminal, Dat = Dat }; //SecureJsonService.CompressEncryptAndSend(); //发送字符串数据 try { await tcpClient.SendAsync(JsonSerializer.Serialize(dat)); } catch (Exception) { await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "请求异常", "是"); } } public static async Task TcpClient(TcpClient tcpClient, string ip) { tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器 tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。 #region Tcp客户端使用Received异步委托接收数据 tcpClient.Received = (client, e) => { //从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。 var mes = e.Memory.Span.ToString(Encoding.UTF8); try { var data = JsonSerializer.Deserialize>(mes); if (data != null) { //处理服务器发送信息 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("UserData", out var value_UserData)) { } if (data.TryGetValue("SysData", out var value_SysData)) { } if (data.TryGetValue("Notification", out val) && val != null)//信息接受 { var dataNotification = JsonSerializer.Deserialize>(val.ToString()!); string _Title = "", _Content = ""; int _Type = 0; if (dataNotification!.TryGetValue("Title", out var value_Notification_Title)) { _Title = value_Notification_Title.ToString() ?? ""; } if (dataNotification!.TryGetValue("Content", out var value_Notification_Content)) { _Content = value_Notification_Content.ToString() ?? ""; } if (dataNotification!.TryGetValue("Type", out var value_Notification_Type)) { string temp = value_Notification_Type?.ToString() ?? ""; // 转换 _Type = temp switch { "System" => 2, "Message" => 1, _ => 0 }; } Models.AppModels.INSERT("INSERT INTO Notification (Title,Content,Time,Type)VALUES('" + _Title + "','" + _Content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + _Type + "');"); } } } catch (Exception) { } return EasyTask.CompletedTask; }; #endregion try { await tcpClient.SetupAsync(new TouchSocketConfig() .SetRemoteIPHost(ip)); await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 } catch (Exception) { await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "不合法的访问地址", "是"); } } } }