Browse Source

字符串错误修正

master
Administrator 3 months ago
parent
commit
155aff76c7
  1. 25
      Class/TcpServer.cs

25
Class/TcpServer.cs

@ -45,15 +45,18 @@ namespace SunlightAggregationTerminal.Class
public static TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient(); public static TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient();
public static void Configuration(string ip, string Name, string Password, string Terminal) public static async void Configuration(string ip, string Name, string Password, string Terminal)
{ {
ServerIP = ip; ServerIP = ip;
ServerName = Name; ServerName = Name;
ServerPassword = Password; ServerPassword = Password;
ServerTerminal = Terminal; ServerTerminal = Terminal;
_ = TcpClient(tcpClient, ServerIP); await TcpClient(tcpClient, ServerIP);
} }
// 引入发送锁,彻底解决连续发送导致的并发断连问题
private static readonly SemaphoreSlim _sendLock = new SemaphoreSlim(1, 1);
public static async void TcpTransmit(string Dat) public static async void TcpTransmit(string Dat)
{ {
var dat = new Person() var dat = new Person()
@ -65,6 +68,9 @@ namespace SunlightAggregationTerminal.Class
Dat = Dat Dat = Dat
}; };
// 使用锁确保同一时间只有一个任务在发送,防止并发写入导致 Socket 异常
await _sendLock.WaitAsync();
//SecureJsonService.CompressEncryptAndSend(); //SecureJsonService.CompressEncryptAndSend();
//发送字符串数据 //发送字符串数据
try try
@ -75,6 +81,10 @@ namespace SunlightAggregationTerminal.Class
{ {
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", ex.Message.ToString(), "是"); await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", ex.Message.ToString(), "是");
} }
finally
{
_sendLock.Release(); // 释放锁
}
} }
@ -82,10 +92,13 @@ namespace SunlightAggregationTerminal.Class
{ {
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) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 tcpClient.Closing = (client, e) => {
tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。 Application.Current!.Windows[0].Page!.DisplayAlertAsync("退出", "即将断开连接", "是");
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) =>
{ {
@ -163,7 +176,7 @@ namespace SunlightAggregationTerminal.Class
} }
} }
catch (Exception) { } catch (Exception ex) { Application.Current!.Windows[0].Page!.DisplayAlertAsync("", ex.Message.ToString(), "是"); }
return EasyTask.CompletedTask; return EasyTask.CompletedTask;
}; };

Loading…
Cancel
Save