WebSocketFlow 1.0.3
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package WebSocketFlow --version 1.0.3
NuGet\Install-Package WebSocketFlow -Version 1.0.3
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="WebSocketFlow" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebSocketFlow" Version="1.0.3" />
<PackageReference Include="WebSocketFlow" />
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 WebSocketFlow --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WebSocketFlow, 1.0.3"
#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 WebSocketFlow@1.0.3
#: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=WebSocketFlow&version=1.0.3
#tool nuget:?package=WebSocketFlow&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
WebSocketFlow
Extension methods for System.Net.WebSockets.WebSocket that simplify receiving complete messages and asynchronously enumerating incoming messages.
Features
ReceiveMessageAsync— Receives a complete WebSocket message, automatically assembling all fragments into a singleWebSocketMessage.ReadAllMessagesAsync— Returns anIAsyncEnumerable<WebSocketMessage>that yields complete messages until the socket closes or a cancellation token is triggered.
Target Frameworks
| Framework | Supported |
|---|---|
| .NET Standard 2.0 | ✓ |
| .NET 8.0+ | ✓ |
Installation
dotnet add package WebSocketFlow
Usage
Receive a single message
using WebSocketFlow;
// Default buffer size (4096 bytes)
WebSocketMessage message = await webSocket.ReceiveMessageAsync(cancellationToken);
// Custom buffer size
WebSocketMessage message = await webSocket.ReceiveMessageAsync(bufferSize: 8192, cancellationToken);
Console.WriteLine(Encoding.UTF8.GetString(message.Data.Span));
Enumerate all messages
using WebSocketFlow;
await foreach (var msg in webSocket.ReadAllMessagesAsync(cancellationToken: cancellationToken))
{
Console.WriteLine(Encoding.UTF8.GetString(msg.Data.Span));
}
// Custom buffer size
await foreach (var msg in webSocket.ReadAllMessagesAsync(bufferSize: 8192, cancellationToken))
{
Console.WriteLine(Encoding.UTF8.GetString(msg.Data.Span));
}
Enumeration stops automatically when a close frame is received or the cancellation token is triggered.
WebSocketMessage
| Member | Type | Description |
|---|---|---|
MessageType |
WebSocketMessageType |
Text, Binary, or Close |
Data |
ReadOnlyMemory<byte> |
Fully assembled payload |
CloseReceived |
bool |
true when MessageType == Close |
WebSocketFlow
对 System.Net.WebSockets.WebSocket 的扩展方法,简化完整消息的接收与异步枚举。
功能
ReceiveMessageAsync— 接收一条完整的 WebSocket 消息,自动将所有分片合并为单个WebSocketMessage。ReadAllMessagesAsync— 返回IAsyncEnumerable<WebSocketMessage>,持续产出完整消息,直到连接关闭或取消令牌触发。
目标框架
| 框架 | 支持 |
|---|---|
| .NET Standard 2.0 | ✓ |
| .NET 8.0+ | ✓ |
安装
dotnet add package WebSocketFlow
使用示例
接收单条消息
using WebSocketFlow;
// 默认缓冲区大小(4096 字节)
WebSocketMessage message = await webSocket.ReceiveMessageAsync(cancellationToken);
// 自定义缓冲区大小
WebSocketMessage message = await webSocket.ReceiveMessageAsync(bufferSize: 8192, cancellationToken);
Console.WriteLine(Encoding.UTF8.GetString(message.Data.Span));
枚举所有消息
using WebSocketFlow;
await foreach (var msg in webSocket.ReadAllMessagesAsync(cancellationToken: cancellationToken))
{
Console.WriteLine(Encoding.UTF8.GetString(msg.Data.Span));
}
// 自定义缓冲区大小
await foreach (var msg in webSocket.ReadAllMessagesAsync(bufferSize: 8192, cancellationToken))
{
Console.WriteLine(Encoding.UTF8.GetString(msg.Data.Span));
}
收到关闭帧或取消令牌触发时,枚举自动停止。
WebSocketMessage
| 成员 | 类型 | 说明 |
|---|---|---|
MessageType |
WebSocketMessageType |
Text、Binary 或 Close |
Data |
ReadOnlyMemory<byte> |
完整合并后的消息内容 |
CloseReceived |
bool |
MessageType == Close 时为 true |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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.
-
.NETStandard 2.0
- Microsoft.Bcl.AsyncInterfaces (>= 10.0.8)
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
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.