change - TcpServer添加通知事件

pull/1/head
wenjy 8 months ago
parent 59da4ec243
commit fe2b7bc742

@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic;
using System.Text; using System.Text;
using TouchSocket.Core; using TouchSocket.Core;
using TouchSocket.Sockets; using TouchSocket.Sockets;
@ -10,6 +11,14 @@ namespace SlnMesnac.TouchSocket
{ {
private ILogger<TcpServer> _logger; private ILogger<TcpServer> _logger;
private readonly TcpService _service; private readonly TcpService _service;
/// <summary>
/// 接收客户端指令委托
/// </summary>
public delegate void ReceivedClientBuffer(byte[] buffer);
public event ReceivedClientBuffer? ReceivedClientBufferEvent;
public delegate void RefreshClientInfo(TcpService tcpService);
public event RefreshClientInfo? RefreshClientInfoEvent;
public TcpServer(ILogger<TcpServer> logger,TcpService tcpService) public TcpServer(ILogger<TcpServer> logger,TcpService tcpService)
{ {
@ -27,21 +36,23 @@ namespace SlnMesnac.TouchSocket
}; };
_service.Connected = (client, e) => { _service.Connected = (client, e) => {
_logger.LogInformation($"客户端{client.IP}接入服务成功"); _logger.LogInformation($"客户端{client.IP}接入服务成功");
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);
return EasyTask.CompletedTask; return EasyTask.CompletedTask;
}; };
_service.Received = (client, e) => _service.Received = (client, e) =>
{ {
//if (requestInfo is MyFixedHeaderRequestInfo myRequestInfo)
//{
// string body = Encoding.UTF8.GetString(myRequestInfo.Body, 0, myRequestInfo.Body.Length);
//}
//从客户端收到信息 //从客户端收到信息
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
byte[] receivedBuffer = new byte[e.ByteBlock.Len];
Array.Copy(e.ByteBlock.Buffer, 0, receivedBuffer, 0, e.ByteBlock.Len);
ReceivedClientBufferEvent?.Invoke(receivedBuffer);
return EasyTask.CompletedTask; return EasyTask.CompletedTask;
}; };
@ -60,10 +71,22 @@ namespace SlnMesnac.TouchSocket
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError($"TcpServer启动异常{ex}"); //throw new InvalidOperationException($"TcpServer启动异常{ex.Message}");
throw new Exception(ex.Message); _logger.LogError($"TcpServer启动异常{ex.Message}");
} }
} }
/// <summary>
/// 向所有客户端发送心跳
/// </summary>
public void SendHeartBeat()
{
var clients = _service.SocketClients.GetClients();
foreach (var item in clients)
{
_service.Send(item.Id,"heartbeat");
}
}
} }
} }

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Builder;
using TouchSocket.Sockets; using TouchSocket.Sockets;
namespace SlnMesnac.TouchSocket namespace SlnMesnac.TouchSocket
@ -16,5 +17,12 @@ namespace SlnMesnac.TouchSocket
services.AddSingleton<TcpService>();//注册TouchSocket的服务 services.AddSingleton<TcpService>();//注册TouchSocket的服务
services.AddSingleton<TcpServer>(); services.AddSingleton<TcpServer>();
} }
public static IApplicationBuilder UseTouchSocketExtensions(this IApplicationBuilder app)
{
var _server = app.ApplicationServices.GetService<TcpServer>();
_server.Init(20108);
return app;
}
} }
} }
Loading…
Cancel
Save