sc 11 months ago
parent
commit
f92275f2d9
  1. 3
      Properties/Resources.zh-TW.resx
  2. 25
      UserClass/AsyncTcpClient.cs
  3. 14
      ViewModel/MainWindowViewModel.cs

3
Properties/Resources.zh-TW.resx

@ -924,4 +924,7 @@
<data name="Today" xml:space="preserve"> <data name="Today" xml:space="preserve">
<value>今天</value> <value>今天</value>
</data> </data>
<data name="Test" xml:space="preserve">
<value>測試</value>
</data>
</root> </root>

25
UserClass/AsyncTcpClient.cs

@ -23,6 +23,8 @@ using Newtonsoft.Json;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
using SunlightCentralizedControlManagement_SCCM_.View; using SunlightCentralizedControlManagement_SCCM_.View;
using TcpClient = TouchSocket.Sockets.TcpClient; using TcpClient = TouchSocket.Sockets.TcpClient;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
namespace SunlightCentralizedControlManagement_SCCM_.UserClass namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{/// <summary> {/// <summary>
@ -30,12 +32,18 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
/// </summary> /// </summary>
public class AsyncTcpClient 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 tcpClient = new TcpClient();
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) =>
{ {
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; return EasyTask.CompletedTask;
};//成功连接到服务器 };//成功连接到服务器
@ -58,13 +66,24 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
.SetRemoteIPHost(ip+":"+port) .SetRemoteIPHost(ip+":"+port)
.ConfigurePlugins(a => .ConfigurePlugins(a =>
{ {
a.UseCheckClear()
.SetCheckClearType(CheckClearType.All)
.SetTick(TimeSpan.FromSeconds(30))
.SetOnClose((c, t) =>
{
c.TryShutdown();
});
a.UseTcpReconnection();//触发型重连 a.UseTcpReconnection();//触发型重连
}) })
.ConfigureContainer(a => .ConfigureContainer(a =>
{ {
// a.AddConsoleLogger();//添加一个日志注入 // a.AddConsoleLogger();//添加一个日志注入
})); }));
try
// Result result = await
_ = tcpClient.TryConnectAsync();
// return result.IsSuccess;
/* try
{ {
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
return true; return true;
@ -72,7 +91,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
catch catch
{ {
return false; return false;
} }*/
} }
} }

14
ViewModel/MainWindowViewModel.cs

@ -106,9 +106,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
private readonly TcpClient[] MachiensTcpClient = new TcpClient[999]; private readonly TcpClient[] MachiensTcpClient = new TcpClient[999];
public int Machinesdata_Count; public int Machinesdata_Count;
public async void TcpClientNEW() public void TcpClientNEW()
{ {
bool Machine_ = false; // bool Machine_ ;
DataRow[] machinesdata = Machines.Select("PORT>0 AND IP<>''", "id asc");//获取连接有效的组 DataRow[] machinesdata = Machines.Select("PORT>0 AND IP<>''", "id asc");//获取连接有效的组
Machinesdata_Count = machinesdata.Count(); Machinesdata_Count = machinesdata.Count();
@ -116,12 +116,10 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
{ {
MachiensTcpClient[i] = new TcpClient(); MachiensTcpClient[i] = new TcpClient();
DataRow dt = machinesdata[i]; DataRow dt = machinesdata[i];
Updata_Machines(Machines, Convert.ToInt16(dt["ID"]), "State", "800");
Machine_ = await AsyncTcpClient.TcpClient(MachiensTcpClient[i] //建立tcp连接 _ = AsyncTcpClient.TcpClient(MachiensTcpClient[i] //建立tcp连接
, Selet_Machines(MainWindowViewModel.Machines, "IP", Convert.ToInt16(dt["ID"])).ToString() , Selet_Machines(Machines, "IP", Convert.ToInt16(dt["ID"])).ToString()
, Selet_Machines(MainWindowViewModel.Machines, "PORT", Convert.ToInt16(dt["ID"])).ToString()); , Selet_Machines(Machines, "PORT", Convert.ToInt16(dt["ID"])).ToString());
if (!Machine_) Updata_Machines(Machines , Convert.ToInt16(dt["ID"]), "State" ,"309" );
} }
} }

Loading…
Cancel
Save