This commit is contained in:
Administrator committed 2026-05-13 02:02:26 +08:00
commit 9694a80c89
2 files changed
+29 -10

No files matched your search

+10 -8
View File
@@ -71,7 +71,6 @@ namespace SunlightAggregationTerminal.Class
// 使用锁确保同一时间只有一个任务在发送,防止并发写入导致 Socket 异常
await _sendLock.WaitAsync();
//SecureJsonService.CompressEncryptAndSend();
//发送字符串数据
try
{
@@ -87,18 +86,14 @@ namespace SunlightAggregationTerminal.Class
}
}
public static async Task TcpClient(TouchSocket.Sockets.TcpClient tcpClient, string ip)
{
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
tcpClient.Connected = (client, e) => {
TcpServer.TcpTransmit("{\"Command\":\"Log\"}");
return EasyTask.CompletedTask; };//成功连接到服务器
tcpClient.Closing = (client, e) => {
Application.Current!.Windows[0].Page!.DisplayAlertAsync("退出", "即将断开连接", "是");
return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
tcpClient.Closed = (client, e) => {
Application.Current!.Windows[0].Page!.DisplayAlertAsync("退出", "连接已断开", "是"); return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
tcpClient.Closing = (client, e) => {return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
tcpClient.Closed = (client, e) => {return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
#region Tcp客户端使用Received异步委托接收数据
tcpClient.Received = (client, e) =>
{
@@ -185,7 +180,14 @@ namespace SunlightAggregationTerminal.Class
try
{
await tcpClient.SetupAsync(new TouchSocketConfig()
.SetRemoteIPHost(ip));
.SetRemoteIPHost(ip)
.ConfigurePlugins(a =>
{
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options =>
{
options.PollingInterval = TimeSpan.FromSeconds(5);
});
}));
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
}
catch (Exception ex)