DotNetty.Extensions 3.0.0

dotnet add package DotNetty.Extensions --version 3.0.0
                    
NuGet\Install-Package DotNetty.Extensions -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotNetty.Extensions" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetty.Extensions" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DotNetty.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotNetty.Extensions --version 3.0.0
                    
#r "nuget: DotNetty.Extensions, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DotNetty.Extensions@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetty.Extensions&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DotNetty.Extensions&version=3.0.0
                    
Install as a Cake Tool

DotNetty.Extensions Base On DotNetty

For Tcp、Udp、WebSocket Both Server And Client

TcpServer

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Extensions;
using DotNetty.Transport.Channels;
using System.Net;
using System.Text;

var server = new TcpSocketServer(8000);

server.OnCreateBootstrap += bootstrap =>
{
    bootstrap.Option(ChannelOption.TcpNodelay, true);
};

server.OnChannelPipeline += pipeline =>
{
    //心跳
    //pipeline.AddLast(new IdleStateHandler(60, 0, 0));

    //编码解码器
    pipeline.AddLast(new LengthFieldPrepender(2));
    pipeline.AddLast(new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));

    pipeline.AddLast(new MyStringDecoder());

    //tls证书
    //var cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
    //pipeline.AddLast(TlsHandler.Server(cert));
};

server.OnStarting += () =>
{
    Console.WriteLine("Starting...");
};

server.OnStarted += () =>
{
    Console.WriteLine("Started.");
};

server.OnStopped += async ex =>
{
    Console.WriteLine($"Stopped==>{ex?.Message}");
    await Task.Delay(2000);
    await server.StartAsync(); //restart
};

server.OnClientConnected += cnn =>
{
    var addr = (IPEndPoint)cnn.Channel.RemoteAddress;

    Console.WriteLine($"ClientConnected==>{cnn.Id}==>IP:{addr.Address}==>Port:{addr.Port}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

server.OnClientMessage += async (cnn, message) =>
{
    if (message is IByteBuffer buffer)
    {
        var txt = buffer.ToString(Encoding.UTF8);
        Console.WriteLine($"ClientMessage==>{cnn.Id}===>{txt}");
    }
    else if (message is string str)
    {
        Console.WriteLine($"ClientMessage==>{cnn.Id}===>{str}");
    }

    var txtSend = Encoding.UTF8.GetBytes("this is from tcp server message!");
    await cnn.WriteAndFlushAsync(txtSend);
};

server.OnClientException += (cnn, ex) =>
{
    Console.WriteLine($"ClientException==>{cnn.Id}==>{ex?.Message}");
};

server.OnClientDisconnected += cnn =>
{
    Console.WriteLine($"ClientDisconnected==>{cnn.Id}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

await server.StartAsync();

Console.ReadKey();

await server.DisposeAsync();

Console.WriteLine("Hello, World!");


public class MyStringDecoder : MessageToMessageDecoder<IByteBuffer>
{
    protected override void Decode(IChannelHandlerContext ctx, IByteBuffer message, List<object> output)
    {
        // 直接将IByteBuffer转为字符串
        output.Add(message.ToString(Encoding.UTF8));
    }
}

TcpClient

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Extensions;
using DotNetty.Transport.Channels;
using System.Text;

var client = new TcpSocketClient("127.0.0.1", 8000);

client.OnChannelPipeline += pipeline =>
{
    //心跳
    //pipeline.AddLast(new IdleStateHandler(5, 0, 0));

    //编码解码器
    pipeline.AddLast(new LengthFieldPrepender(2));
    pipeline.AddLast(new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));

    pipeline.AddLast(new MyStringDecoder());

    //tls证书
    //var cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
    //var targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
    //pipeline.AddLast(new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(targetHost)));

};

client.OnConnecting += () =>
{
    Console.WriteLine("Connecting...");
};

client.OnConnected += async () =>
{
    Console.WriteLine("Connected.");

    var txt = Encoding.UTF8.GetBytes("this is from tcp client message!");
    await client.WriteAndFlushAsync(txt);
};

client.OnMessage += message =>
{
    if (message is IByteBuffer buffer)
    {
        var txt = buffer.ToString(Encoding.UTF8);
        Console.WriteLine($"Message==>{txt}");
    }
    else if (message is string str)
    {
        Console.WriteLine($"Message==>{str}");
    }
};

client.OnException += ex =>
{
    Console.WriteLine($"Exception=>{ex?.Message}");
};

client.OnDisconnected += async () =>
{
    Console.WriteLine("Disconnected.");
    await Task.Delay(2000);
    await client.ConnectAsync(); //reconnect
};

await client.ConnectAsync();

Console.ReadKey();

await client.DisposeAsync();

Console.WriteLine("Hello, World!");



public class MyStringDecoder : MessageToMessageDecoder<IByteBuffer>
{
    protected override void Decode(IChannelHandlerContext ctx, IByteBuffer message, List<object> output)
    {
        // 直接将IByteBuffer转为字符串
        output.Add(message.ToString(Encoding.UTF8));
    }
}

UDP

using DotNetty.Extensions;
using System.Net;
using System.Text;

var udp = new UdpSocket(8888);

udp.OnStarting += () =>
{
    Console.WriteLine("Starting...");
};

udp.OnStarted += async () =>
{
    Console.WriteLine("Started.");

    var endPoint = new IPEndPoint(IPAddress.Broadcast, 7777);
    var bytes = Encoding.UTF8.GetBytes("this is from UDP2 message!");
    await udp.WriteAndFlushAsync(endPoint, bytes);

    await udp.WriteAndFlushAsync("127.0.0.1:7777", bytes);
};

udp.OnStopped += async ex =>
{
    Console.WriteLine($"Stopped==>{ex?.Message}");
    await Task.Delay(2000);
    await udp.StartAsync(); //reconnect
};

udp.OnException += ex =>
{
    Console.WriteLine($"Exception==>{ex?.Message}");
};

udp.OnMessage += (endPoint, bytes) =>
{
    Console.WriteLine(endPoint);
    Console.WriteLine(Encoding.UTF8.GetString(bytes));

};

await udp.StartAsync();

Console.ReadKey();

await udp.DisposeAsync();

Console.WriteLine("Hello, World!");

WebSocketServer

using DotNetty.Extensions;
using System.Net;
using System.Text;

var server = new WebSocketServer(8000);

//server.Path = "/kkk";

server.OnChannelPipeline += pipeline =>
{
    //心跳
    //pipeline.AddLast(new IdleStateHandler(60, 0, 0));

    //tls证书
    //var cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
    //pipeline.AddLast(TlsHandler.Server(cert));
};

server.OnStarting += () =>
{
    Console.WriteLine("Starting...");
};

server.OnStarted += () =>
{
    Console.WriteLine("Started.");
};

server.OnStopped += async ex =>
{
    Console.WriteLine($"Stopped==>{ex?.Message}");
    await Task.Delay(2000);
    await server.StartAsync(); //restart
};

server.OnClientConnected += cnn =>
{
    var addr = (IPEndPoint)cnn.Channel.RemoteAddress;

    Console.WriteLine($"ClientConnected==>{cnn.Id}==>IP:{addr.Address}==>Port:{addr.Port}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

server.OnClientTextMessage += async (cnn, message) =>
{
    Console.WriteLine($"ClientTextMessage==>{cnn.Id}===>{message}");

    await cnn.WriteAndFlushAsync("this is from websocket server text message!");
};

server.OnClientBinaryMessage += async (cnn, bytes) =>
{
    var txt = Encoding.UTF8.GetString(bytes);
    Console.WriteLine($"ClientBinaryMessage==>{cnn.Id}===>{txt}");

    var bs = Encoding.UTF8.GetBytes("this is from websocket server binary message!");
    await cnn.WriteAndFlushAsync(bs);
};

server.OnClientException += (cnn, ex) =>
{
    Console.WriteLine($"ClientException==>{cnn.Id}==>{ex?.Message}");
};

server.OnClientDisconnected += cnn =>
{
    Console.WriteLine($"ClientDisconnected==>{cnn.Id}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

await server.StartAsync();

Console.ReadKey();

await server.DisposeAsync();

Console.WriteLine("Hello, World!");

WebSocketClient

using DotNetty.Extensions;
using System.Text;

var client = new WebSocketClient("ws://127.0.0.1:8000");

client.OnChannelPipeline += pipeline =>
{
    //心跳
    //pipeline.AddLast(new IdleStateHandler(5, 0, 0));

    //tls证书
    //var cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
    //var targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
    //pipeline.AddLast(new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(targetHost)));

};

client.OnConnecting += () =>
{
    Console.WriteLine("Connecting...");
};

client.OnConnected += async () =>
{
    Console.WriteLine("Connected.");

    await client.WriteAndFlushAsync("this is from websocket client text message!");

    var bs = Encoding.UTF8.GetBytes("this is from websocket client binary message!");

    await client.WriteAndFlushAsync(bs);

};

client.OnTextMessage += message =>
{
    Console.WriteLine($"TextMessage==>{message}");
};

client.OnBinaryMessage += message =>
{
    var txt = Encoding.UTF8.GetString(message);
    Console.WriteLine($"BinaryMessage==>{txt}");
};

client.OnException += ex =>
{
    Console.WriteLine($"Exception=>{ex?.Message}");
};

client.OnDisconnected += async () =>
{
    Console.WriteLine("Disconnected.");
    await Task.Delay(2000);
    await client.ConnectAsync(); //reconnect
};

await client.ConnectAsync();

Console.ReadKey();

await client.DisposeAsync();

Console.WriteLine("Hello, World!");

TcpSocketServer Libuv

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Extensions;
using DotNetty.Transport.Channels;
using System.Net;
using System.Text;

var server = new TcpSocketServerLibuv(8000);

server.OnCreateBootstrap += bootstrap =>
{
    bootstrap.Option(ChannelOption.TcpNodelay, true);
};

server.OnChannelPipeline += pipeline =>
{
    //心跳
    //pipeline.AddLast(new IdleStateHandler(60, 0, 0));

    //编码解码器
    pipeline.AddLast(new LengthFieldPrepender(2));
    pipeline.AddLast(new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));

    //tls证书
    //var cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
    //pipeline.AddLast(TlsHandler.Server(cert));
};

server.OnStarting += () =>
{
    Console.WriteLine("Starting...");
};

server.OnStarted += () =>
{
    Console.WriteLine("Started.");
};

server.OnStopped += async ex =>
{
    Console.WriteLine($"Stopped==>{ex?.Message}");
    await Task.Delay(2000);
    await server.StartAsync(); //restart
};

server.OnClientConnected += cnn =>
{
    var addr = (IPEndPoint)cnn.Channel.RemoteAddress;

    Console.WriteLine($"ClientConnected==>{cnn.Id}==>IP:{addr.Address}==>Port:{addr.Port}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

server.OnClientMessage += async (cnn, message) =>
{
    var buffer = (IByteBuffer)message;
    var txt = buffer.ToString(Encoding.UTF8);
    Console.WriteLine($"ClientMessage==>{cnn.Id}===>{txt}");

    var txtSend = Encoding.UTF8.GetBytes("this is from tcp server message!");
    await cnn.WriteAndFlushAsync(txtSend);
};

server.OnClientException += (cnn, ex) =>
{
    Console.WriteLine($"ClientException==>{cnn.Id}==>{ex?.Message}");
};

server.OnClientDisconnected += cnn =>
{
    Console.WriteLine($"ClientDisconnected==>{cnn.Id}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

await server.StartAsync();

Console.ReadKey();

await server.DisposeAsync();

Console.WriteLine("Hello, World!");

WebSocketServer Libuv

using DotNetty.Extensions;
using System.Net;
using System.Text;

var server = new WebSocketServerLibuv(8000);

//server.Path = "/kkk";

server.OnChannelPipeline += pipeline =>
{
    //心跳
    //pipeline.AddLast(new IdleStateHandler(60, 0, 0));

    //tls证书
    //var cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
    //pipeline.AddLast(TlsHandler.Server(cert));
};

server.OnStarting += () =>
{
    Console.WriteLine("Starting...");
};

server.OnStarted += () =>
{
    Console.WriteLine("Started.");
};

server.OnStopped += async ex =>
{
    Console.WriteLine($"Stopped==>{ex?.Message}");
    await Task.Delay(2000);
    await server.StartAsync(); //restart
};

server.OnClientConnected += cnn =>
{
    var addr = (IPEndPoint)cnn.Channel.RemoteAddress;

    Console.WriteLine($"ClientConnected==>{cnn.Id}==>IP:{addr.Address}==>Port:{addr.Port}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

server.OnClientTextMessage += async (cnn, message) =>
{
    Console.WriteLine($"ClientTextMessage==>{cnn.Id}===>{message}");

    await cnn.WriteAndFlushAsync("this is from websocket server text message!");
};

server.OnClientBinaryMessage += async (cnn, bytes) =>
{
    var txt = Encoding.UTF8.GetString(bytes);
    Console.WriteLine($"ClientBinaryMessage==>{cnn.Id}===>{txt}");

    var bs = Encoding.UTF8.GetBytes("this is from websocket server binary message!");
    await cnn.WriteAndFlushAsync(bs);
};

server.OnClientException += (cnn, ex) =>
{
    Console.WriteLine($"ClientException==>{cnn.Id}==>{ex?.Message}");
};

server.OnClientDisconnected += cnn =>
{
    Console.WriteLine($"ClientDisconnected==>{cnn.Id}");
    Console.WriteLine($"ClientCount:{server.ClientDict.Count}");
};

await server.StartAsync();

Console.ReadKey();

await server.DisposeAsync();

Console.WriteLine("Hello, World!");

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 118 7/16/2025