Ekstensi yang memungkinkan Anda memutar server Enet di aplikasi ASP.NET Core Anda. Ini didasarkan pada karya mengagumkan Stanislav Denisov: ENet-CSharp.
Sangat mudah untuk mengaturnya:
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...
}
Anda dapat menemukan contoh penggunaan di folder MHLab.AspNetCore.Enet.Sample
. Ini adalah template ASP.NET Core Web API klasik dengan tambahan Enet Server.
Pengendali koneksi dipicu ketika koneksi klien baru dibuat. Anda dapat membangun penangan koneksi seperti ini:
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 } " ) ;
}
}
Penangan pemutusan dipicu ketika klien yang sudah terhubung terputus dengan baik. Anda dapat membuat penangan pemutusan hubungan seperti ini:
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 } " ) ;
}
}
Penangan batas waktu dipicu ketika klien yang sudah terhubung terputus secara tidak terkendali. Anda dapat membuat penangan batas waktu seperti ini:
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 } " ) ;
}
}
Penangan paket dipicu ketika pesan diterima dari klien yang terhubung. Anda dapat membangun penangan paket seperti ini:
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 } " ) ;
}
}