Networker
1.0.0
一個簡單易用的 .NET TCP 和 UDP 網路庫,設計靈活、可擴充且快速。
NuGet 套件管理器
Install-Package Networker
然後,您必須安裝以下格式化程式之一
Install-Package Networker.Extensions.ZeroFormatter
Install-Package Networker.Extensions.MessagePack
Install-Package Networker.Extensions.ProtoBufNet
Install-Package Networker.Extensions.Json
開始學習由庫開發人員 Mark Eastwood 編寫的本教程
建立伺服器很容易..
var server = new ServerBuilder ( )
. UseTcp ( 1000 )
. UseUdp ( 5000 )
. RegisterPacketHandlerModule < DefaultPacketHandlerModule > ( )
. RegisterPacketHandlerModule < ExamplePacketHandlerModule > ( )
. UseZeroFormatter ( )
. ConfigureLogging ( loggingBuilder =>
{
loggingBuilder . AddConsole ( ) ;
loggingBuilder . SetMinimumLevel (
LogLevel . Debug ) ;
} )
. Build ( ) ;
server . Start ( ) ;
您可以使用依賴項注入、日誌記錄和內建反序列化輕鬆處理資料包。
public class ChatPacketHandler : PacketHandlerBase < ChatPacket >
{
private readonly ILogger < ChatPacketHandler > _logger ;
public ChatPacketHandler ( ILogger < ChatPacketHandler > logger )
{
_logger = logger ;
}
public override async Task Process ( ChatPacket packet , IPacketContext packetContext )
{
_logger . LogDebug ( "I received the chat message: " + packet . Message ) ;
packetContext . Sender . Send ( new ChatPacket
{
Message = "Hey, I got your message!"
} ) ;
}
}