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