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

21
InfoPage.xaml.cs

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

Loading…
Cancel
Save