diff --git a/Class/AsyncTcpServer.cs b/Class/AsyncTcpServer.cs index a6c6cf9..692853b 100644 --- a/Class/AsyncTcpServer.cs +++ b/Class/AsyncTcpServer.cs @@ -30,7 +30,17 @@ namespace ForwardingServerUI.Class //TcpService service = new TcpService(); service.Connecting = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在连接 service.Connected = (client, e) => {return EasyTask.CompletedTask;};//有客户端成功连接 - service.Closing = (client, e) => {return EasyTask.CompletedTask; };//有客户端正在断开连接,只有当主动断开时才有效。 + service.Closing = (client, e) => { //离线记录 + DataRow[] dat = MainWindowViewModel._machineTable.Select("ID='" + client.Id + "'"); + foreach (DataRow row in dat) + { + Application.Current.Dispatcher.Invoke(() => { + MainWindowViewModel._machineTable.Rows.Remove(row); + }); + } + Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "] NOT ID " + client.Id + " IP " + client.IP); + + return EasyTask.CompletedTask; };//有客户端正在断开连接,只有当主动断开时才有效。 service.Closed = (client, e) => { //离线记录 DataRow[] dat = MainWindowViewModel._machineTable.Select("ID='"+client.Id+"'"); @@ -40,7 +50,7 @@ namespace ForwardingServerUI.Class MainWindowViewModel._machineTable.Rows.Remove(row); }); } - Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + "] NOT ID " + client.Id + " IP " + client.IP); + Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "] NOT ID " + client.Id + " IP " + client.IP); return EasyTask.CompletedTask; };//有客户端断开连接 @@ -62,6 +72,7 @@ namespace ForwardingServerUI.Class (trimmedDat[0] == '{' || trimmedDat[0] == '[')) { MainWindowViewModel.Person deserializedPerson = JsonSerializer.Deserialize(trimmedDat)!; + //转发逻辑 var dr = MainWindowViewModel._userTable.Select("Terminal='" + deserializedPerson.Terminal + "'"); if (dr.Length > 0) @@ -74,16 +85,28 @@ namespace ForwardingServerUI.Class } } - } else if (s_ == 90) { } + } + else if (s_ == 90) + { } + Application.Current.Dispatcher.Invoke(() => + { + DataRow drEmployee = r_.First(); + drEmployee.BeginEdit(); + drEmployee["HeartbeatTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); + drEmployee.EndEdit(); + drEmployee.AcceptChanges(); + }); } else { Personm deserializedPerson = JsonSerializer.Deserialize(DAT)!; - Application.Current.Dispatcher.Invoke(() => { - var mach = MainWindowViewModel._machineTable.Select("Enterprise='" + deserializedPerson.SERVEREnterprise + - "'and MachineName='" + deserializedPerson.SERVERMachineName + "'"); + Application.Current.Dispatcher.Invoke(() => + { + var mach = MainWindowViewModel._machineTable.Select("Enterprise='" + deserializedPerson.SERVEREnterprise + + "'and MachineName='" + deserializedPerson.SERVERMachineName + "'"); if (mach.Length > 0) - {//刷新设备信息表 + { + //刷新设备信息表 DataRow dataRow = mach.First(); dataRow.BeginEdit(); dataRow["ID"] = client.Id; @@ -96,14 +119,14 @@ namespace ForwardingServerUI.Class dataRow.AcceptChanges(); } else - {//添加设备信息表 - MainWindowViewModel._machineTable.Rows.Add(client.Id, client.IP, 30, "/" + deserializedPerson.SERVEREnterprise, - deserializedPerson.SERVERMachineName, deserializedPerson.MachineVer); - + { + //添加设备信息表 + MainWindowViewModel._machineTable.Rows.Add(client.Id, client.IP, 30, "/" + deserializedPerson.SERVEREnterprise, + deserializedPerson.SERVERMachineName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), deserializedPerson.MachineVer); } }); - Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + " [LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT); - + Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " [LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT); + } } } @@ -133,7 +156,50 @@ namespace ForwardingServerUI.Class await service.StartAsync();//启动 Console.WriteLine("TcpServer:Run"); - } - + + Timer timer = new Timer(TimerCallback, null, TimeSpan.FromSeconds(90), TimeSpan.FromSeconds(30)); + } + + private static async void TimerCallback(object? state) + { + try + { + int rows = MainWindowViewModel._machineTable.Rows.Count; + for (int i = rows; i > 0; i--) + { + DataRow dataRow = MainWindowViewModel._machineTable.Rows[i]; + DateTime dateTime = DateTime.Parse(dataRow["HeartbeatTime"].ToString()!); + if (DateTime.Now > dateTime.AddSeconds(180)) + { + //标记无回应连接 + await Application.Current.Dispatcher.Invoke(async () => + { + dataRow.BeginEdit(); + dataRow["Start"] = 999; + dataRow.EndEdit(); + dataRow.AcceptChanges(); + }); + + //关闭无回应连接 + foreach (var item in service.Clients) + { + if (item.Id == dataRow["ID"].ToString()) + { + await item.CloseAsync(); + + //在断开连接后,释放对象。 + item.Dispose(); + } + } + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + LogGing.ERRDATA(ex); + } + } + } } diff --git a/MainWindowViewModel.cs b/MainWindowViewModel.cs index b9d80cf..9c8b36e 100644 --- a/MainWindowViewModel.cs +++ b/MainWindowViewModel.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Text; +using TouchSocket.Sockets; namespace ForwardingServerUI { @@ -32,6 +33,7 @@ namespace ForwardingServerUI _machineTable.Columns.Add("Start", typeof(int)); _machineTable.Columns.Add("Enterprise", typeof(string)); _machineTable.Columns.Add("MachineName", typeof(string)); + _machineTable.Columns.Add("HeartbeatTime", typeof(string)); _machineTable.Columns.Add("MachineVer", typeof(string)); @@ -52,5 +54,7 @@ namespace ForwardingServerUI { Data(); } + + } }