54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TouchSocket.Core;
|
|
using TouchSocket.Sockets;
|
|
|
|
namespace WCS.BLL.Tool
|
|
{
|
|
/// <summary>
|
|
/// 一个心跳计数器扩展。
|
|
/// </summary>
|
|
internal static class DependencyExtensions
|
|
{
|
|
public static readonly DependencyProperty<Timer> HeartbeatTimerProperty =
|
|
DependencyProperty<Timer>.Register("HeartbeatTimer", null);
|
|
|
|
public static bool Ping<TClient>(this TClient client) where TClient : ITcpClientBase
|
|
{
|
|
try
|
|
{
|
|
var data = new byte[] { 0x05, 0x00, 0x00, 0x06, 0x78, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x00, 0x00 };
|
|
client.Send(data);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logs.Write($"【{client.IP}:{client.Port}】发送心跳中发现异常:" + ex.Message, LogsType.Info);
|
|
if (ex is NotConnectedException)
|
|
{
|
|
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool Pong<TClient>(this TClient client) where TClient : ITcpClientBase
|
|
{
|
|
try
|
|
{
|
|
client.Send(new MyRequestInfo() { DataType = DataType.Pong }.PackageAsBytes());
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
client.Logger.Exception(ex);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|