This commit is contained in:
sc committed 2024-11-22 22:10:24 +08:00
commit f92275f2d9
3 files changed
+36 -16

No files matched your search

+25 -6
View File
@@ -23,6 +23,8 @@ using Newtonsoft.Json;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
using SunlightCentralizedControlManagement_SCCM_.View;
using TcpClient = TouchSocket.Sockets.TcpClient;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{/// <summary>
@@ -30,13 +32,19 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
/// </summary>
public class AsyncTcpClient
{
public static async Task<bool> TcpClient(TcpClient tcpClient, string ip,string port)
public static async Task TcpClient(TcpClient tcpClient, string ip,string port)
{
//TcpClient tcpClient = new TcpClient();
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
tcpClient.Connected = (client, e) =>
{
tcpClient.Connected = (client, e) =>
{
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First();
drEmployee.BeginEdit();
drEmployee["State"] = "101";
drEmployee.EndEdit();
drEmployee.AcceptChanges();
drEmployee.ClearErrors();
return EasyTask.CompletedTask;
};//成功连接到服务器
tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
@@ -58,13 +66,24 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
.SetRemoteIPHost(ip+":"+port)
.ConfigurePlugins(a =>
{
a.UseCheckClear()
.SetCheckClearType(CheckClearType.All)
.SetTick(TimeSpan.FromSeconds(30))
.SetOnClose((c, t) =>
{
c.TryShutdown();
});
a.UseTcpReconnection();//触发型重连
})
.ConfigureContainer(a =>
{
// a.AddConsoleLogger();//添加一个日志注入
}));
try
// Result result = await
_ = tcpClient.TryConnectAsync();
// return result.IsSuccess;
/* try
{
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
return true;
@@ -72,7 +91,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
catch
{
return false;
}
}*/
}
}