MHLab.AspNetCore.Enet
1.0.0
允许您在 ASP.NET Core 应用程序中旋转 Enet 服务器的扩展。它基于 Stanislav Denisov 的出色作品:ENet-CSharp。
设置很简单:
public void ConfigureServices ( IServiceCollection services )
{
// Your other code...
services . AddEnet ( ( configuration ) =>
{
configuration
/* Set the port your Enet Server will listen to. */
. SetPort ( 7001 )
/* Set the polling time, in milliseconds. */
. SetPollingTimeout ( 15 ) ;
/* Add your connection handlers to compose the pipeline. The order matters. */
configuration
. AddConnectionHandler < MyConnectionHandler1 > ( )
. AddConnectionHandler < MyConnectionHandlerN > ( ) ;
/* Add your disconnection handlers to compose the pipeline. The order matters. */
configuration
. AddDisconnectionHandler < MyDisconnectionHandler1 > ( )
. AddDisconnectionHandler < MyDisconnectionHandlerN > ( ) ;
/* Add your timeout handlers to compose the pipeline. The order matters. */
configuration
. AddTimeoutHandler < MyTimeoutHandler1 > ( )
. AddTimeoutHandler < MyTimeoutHandlerN > ( ) ;
/* Add your packet received handlers to compose the pipeline. The order matters. */
configuration
. AddPacketHandler < MyPacketHandler1 > ( )
. AddPacketHandler < MyPacketHandlerN > ( ) ;
} ) ;
// Your other code...
}
您可以在MHLab.AspNetCore.Enet.Sample
文件夹中找到使用示例。它是经典的 ASP.NET Core Web API 模板,并添加了 Enet 服务器。
当建立新的客户端连接时,将触发连接处理程序。您可以像这样构建连接处理程序:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class ConnectionHandler : IConnectionHandler
{
private readonly ILogger < ConnectionHandler > _logger ;
public ConnectionHandler ( ILogger < ConnectionHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnConnectedPeer ( Peer peer )
{
_logger . LogDebug ( $ "Client connected - ID: { peer . ID } , IP: { peer . IP } " ) ;
}
}
当已连接的客户端正常断开连接时,会触发断开连接处理程序。您可以像这样构建断开连接处理程序:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class DisconnectionHandler : IDisconnectionHandler
{
private readonly ILogger < DisconnectionHandler > _logger ;
public DisconnectionHandler ( ILogger < DisconnectionHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnDisconnectedPeer ( Peer peer )
{
_logger . Log ( LogLevel . Debug , $ "Client disconnected - ID: { peer . ID } , IP: { peer . IP } " ) ;
}
}
当已连接的客户端非正常断开连接时,会触发超时处理程序。您可以像这样构建超时处理程序:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class TimeoutHandler : ITimeoutHandler
{
private readonly ILogger < TimeoutHandler > _logger ;
public TimeoutHandler ( ILogger < TimeoutHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnTimeoutPeer ( Peer peer )
{
_logger . Log ( LogLevel . Debug , $ "Client timeout - ID: { peer . ID } , IP: { peer . IP } " ) ;
}
}
当从连接的客户端接收到消息时,将触发数据包处理程序。您可以像这样构建数据包处理程序:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class PacketHandler : IPacketHandler
{
private readonly ILogger < PacketHandler > _logger ;
public PacketHandler ( ILogger < PacketHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnPacketReceived ( Peer peer , byte channelId , Packet packet )
{
_logger . Log ( LogLevel . Debug , $ "Packet received from - ID: { peer . ID } , IP: { peer . IP } , Channel ID: { channelId } , Data length: { packet . Length } " ) ;
}
}