Websocket.Client.Net 0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Websocket.Client.Net --version 0.0.1
                    
NuGet\Install-Package Websocket.Client.Net -Version 0.0.1
                    
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="Websocket.Client.Net" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Websocket.Client.Net" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Websocket.Client.Net" />
                    
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 Websocket.Client.Net --version 0.0.1
                    
#r "nuget: Websocket.Client.Net, 0.0.1"
                    
#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 Websocket.Client.Net@0.0.1
                    
#: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=Websocket.Client.Net&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Websocket.Client.Net&version=0.0.1
                    
Install as a Cake Tool

Websocket.Client.Net

面向 .NET 8 的原生 WebSocket Client 类库。仅使用 BCL 的 ClientWebSocket,没有第三方运行时依赖;原生支持 ws://、wss://、Header、Cookie、事件通知与自动重连。

快速使用

using Websocket.Client.Net;

await using var ws = new WebSocketClient("wss://example.com/ws", new WebSocketClientOptions
{
    AutoReconnect = true,
    MaxReconnectAttempts = 10,             // -1 表示无限重试
    ReconnectDelay = TimeSpan.FromSeconds(1)
});

ws.SetHeader("Authorization", "Bearer token");
ws.SetHeader("User-Agent", "my-service/1.0");
ws.SetCookie("session", "cookie-value");

ws.OnOpen += (sender, e) =>
{
    Console.WriteLine(e.IsReconnect ? "重连成功" : "连接成功");
};

ws.OnMessage += (sender, e) =>
{
    if (e.IsText)
        Console.WriteLine(e.Text);
    else
        Console.WriteLine($"binary: {e.Data.Length} bytes");
};

ws.OnError += (sender, e) =>
{
    Console.WriteLine($"{e.Operation}: {e.Message}; willReconnect={e.WillReconnect}");
};

ws.OnClose += (sender, e) =>
{
    Console.WriteLine($"{e.Code}: {e.Reason}; clean={e.WasClean}");
};

ws.OnReconnecting += (sender, e) =>
{
    Console.WriteLine($"第 {e.Attempt} 次重连,等待 {e.Delay}");
};

await ws.ConnectAsync();
await ws.SendAsync("hello");
await ws.DisconnectAsync(reason: "shutdown");

二进制发送提供明确重载,全部支持 CancellationToken:

await ws.SendAsync(byteArray, cancellationToken: cancellationToken);
await ws.SendAsync(arraySegment, cancellationToken: cancellationToken);
await ws.SendAsync(readOnlySequence, cancellationToken: cancellationToken);

ConnectAsync(cancellationToken) 的 Token 取消当前调用者的等待,不会取消其他调用者共享的连接或重连流程。发送 Token 若在原生发送开始后取消,当前物理连接会中止,避免后续消息接在不完整的 WebSocket 消息后面;启用自动重连时会建立干净的新连接。

吞吐优先的零拷贝接收

OnMessage 为了保证事件返回后数据仍然有效,会为每条完整消息创建一份稳定的 byte[]。吞吐敏感路径可改用 MessageReceivedAsync;传入的内存来自池,只能在回调完成前使用:

ws.MessageReceivedAsync = async (sender, message, type, cancellationToken) =>
{
    await processor.ProcessAsync(message, cancellationToken);
    // 不要把 message 保存到回调之外;需要保留时请调用 message.ToArray()。
};
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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
0.0.3 131 8/23/2026
0.0.2 118 8/13/2026
0.0.1 125 8/12/2026