Administrator 3 months ago
parent
commit
9694a80c89
  1. 18
      Class/TcpServer.cs
  2. 21
      InfoPage.xaml.cs

18
Class/TcpServer.cs

@ -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)

21
InfoPage.xaml.cs

@ -10,6 +10,7 @@ namespace SunlightAggregationTerminal;
public partial class InfoPage : ContentPage
{
public static ObservableCollection<InfoItem> InfoItems { get; set; } = new();
private DateTime _lastRefreshTime = DateTime.MinValue;
public static string? _Name, _State, _MachineName, _Machine, _Information, _Model,
_Type, _ModelA, _ModelB, _ModelC, _ModelD, _ModelE, _ModelF;
@ -27,8 +28,24 @@ public partial class InfoPage : ContentPage
});
//注册当前页面的下滑刷新
myRefreshView_.Refreshing += (sender, e) => {
TcpServer.TcpTransmit("{\"Command\":\"Info\"}");
myRefreshView_.IsRefreshing = false; };
// 5秒防抖判断:如果距离上次刷新不足5秒,直接取消本次操作
if ((DateTime.Now - _lastRefreshTime).TotalSeconds < 5)
{
myRefreshView_.IsRefreshing = false;
return;
}
try
{
// 更新刷新时间
_lastRefreshTime = DateTime.Now;
TcpServer.TcpTransmit("{\"Command\":\"Info\"}");
}
catch (Exception)
{
}
};
}
private void HandleUITrigger()
{

Loading…
Cancel
Save