注入回调函数

master
nodyang@aliyun.com 2 months ago
parent cd916cd323
commit 0c1214ff48

@ -34,26 +34,35 @@ using JsonSerializer = System.Text.Json.JsonSerializer;
#endregion << 版 本 注 释 >> #endregion << 版 本 注 释 >>
namespace SlnMesnac.TouchSocket namespace SlnMesnac.TouchSocket
{ {
public class TcpServer public class TcpServer
{ {
private ILogger<TcpServer> _logger; private ILogger<TcpServer> _logger;
private readonly TcpService _service; private readonly TcpService _service;
private IScanLogService _scanService; private readonly IScanLogService _scanService;
/// <summary> // /// <summary>
/// 接收客户端指令委托 // /// 接收客户端指令委托
/// </summary> // /// </summary>
public delegate void ReceivedClientBuffer(byte[] buffer); // public delegate void ReceivedClientBuffer(byte[] buffer);
public event ReceivedClientBuffer? ReceivedClientBufferEvent; // public event ReceivedClientBuffer? ReceivedClientBufferEvent;
//
// public delegate void RefreshClientInfo(TcpService tcpService);
// public event RefreshClientInfo? RefreshClientInfoEvent;
public Action<ScanLogSocket> ScanLogSocketAction;
public Action<ScanStatusSocket> ScanStatusSocketAction;
public delegate void RefreshClientInfo(TcpService tcpService);
public event RefreshClientInfo? RefreshClientInfoEvent;
public TcpServer(ILogger<TcpServer> logger,TcpService tcpService, IScanLogService scan) public TcpServer(ILogger<TcpServer> logger,TcpService tcpService, IScanLogService scan)
{ {
_logger = logger; _logger = logger;
_service = tcpService; _service = tcpService;
_scanService = scan; _scanService = scan;
} }
public void Init(int serverPort) public void Init(int serverPort)
@ -66,22 +75,22 @@ namespace SlnMesnac.TouchSocket
}; };
_service.Connected = (client, e) => { _service.Connected = (client, e) => {
_logger.LogInformation($"客户端{client.IP}接入服务成功"); _logger.LogInformation($"客户端{client.IP}接入服务成功");
RefreshClientInfoEvent?.Invoke(_service); // RefreshClientInfoEvent?.Invoke(_service);
return EasyTask.CompletedTask; return EasyTask.CompletedTask;
}; };
_service.Disconnected = (client, e) => { _service.Disconnected = (client, e) => {
_logger.LogInformation($"客户端{client.IP}断开连接"); _logger.LogInformation($"客户端{client.IP}断开连接");
RefreshClientInfoEvent?.Invoke(_service); // RefreshClientInfoEvent?.Invoke(_service);
return EasyTask.CompletedTask; return EasyTask.CompletedTask;
}; };
_service.Received = (client, e) => _service.Received = (client, e) =>
{ {
//从客户端收到信息 //从客户端收到信息
var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意数据长度是byteBlock.Len var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意数据长度是byteBlock.Len
_logger.LogInformation("收到数据:{Data}",mes);
byte[] receivedBuffer = new byte[e.ByteBlock.Len]; // byte[] receivedBuffer = new byte[e.ByteBlock.Len];
Array.Copy(e.ByteBlock.Buffer, 0, receivedBuffer, 0, e.ByteBlock.Len); // Array.Copy(e.ByteBlock.Buffer, 0, receivedBuffer, 0, e.ByteBlock.Len);
ReceivedClientBufferEvent?.Invoke(receivedBuffer); // ReceivedClientBufferEvent?.Invoke(receivedBuffer);
int dataType = 0; int dataType = 0;
var jObject = JObject.Parse(mes); var jObject = JObject.Parse(mes);
@ -94,7 +103,7 @@ namespace SlnMesnac.TouchSocket
{ {
ScanLogSocket scanStatus= JsonConvert.DeserializeObject<ScanLogSocket>(mes); ScanLogSocket scanStatus= JsonConvert.DeserializeObject<ScanLogSocket>(mes);
ScanLogSocketAction?.Invoke(scanStatus);
_scanService.Insert(new ScanLogModel() _scanService.Insert(new ScanLogModel()
{ {
CreateTime = scanStatus.CreateTime, CreateTime = scanStatus.CreateTime,
@ -114,6 +123,7 @@ namespace SlnMesnac.TouchSocket
if (dataType == 2) if (dataType == 2)
{ {
ScanStatusSocket scanStatus= JsonConvert.DeserializeObject<ScanStatusSocket>(mes); ScanStatusSocket scanStatus= JsonConvert.DeserializeObject<ScanStatusSocket>(mes);
ScanStatusSocketAction?.Invoke(scanStatus);
} }
} }

Loading…
Cancel
Save