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 } " ) ;
}
}