注入回调函数

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

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

Loading…
Cancel
Save