System.Net.WebSockets.WebSocketProtocol
Get Started
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard | |
| Other |
README
About
Provides the WebSocketProtocol class, which allows creating a WebSocket from a connected stream using WebSocketsProtocol.CreateFromConnectedStream.
NOTE: This package was designed as a temporary solution for internal use (building ASP.NET targeting .NET Standard 2.0), and is now considered obsolete. If targeting .NET Standard 2.1 or .NET 5+, use WebSocket.CreateFromStream instead.
Key Features
- Creates a new
WebSocketinstance that operates on the specified transport stream. TheWebSocketclass allows applications to send and receive data after theWebSocketupgrade has completed. - The API can be used to create both client-side and server-side
WebSocketinstances.
How to Use
To create a WebSocket using WebSocketProtocol, as a prerequisite, you need to prepare the opaque transport stream for the WebSocket, for example, create or accept a TCP socket connection as a Stream and perform a WebSocket opening handshake (upgrade) over it. Then you can use the transport stream and the negotiated subprotocol to call WebSocketProtocol.CreateFromStream.
Stream opaqueTransport = /* ... */; // complete WebSocket opening handshake over the transport connection
WebSocket ws = WebSocketProtocol.CreateFromStream(opaqueTransport, isServer: true, subProtocol, keepAliveInterval);
Remarks
The API is considered obsolete and is not recommented for use.
On .NET Framework or .NET Standard 2.0:
- To create a client
WebSocketfrom a stream, useWebSocket.CreateClientWebSocket.- Alternatively, consider using a
ClientWebSocketclass.
- Alternatively, consider using a
- Creating a server
WebSocketfrom a stream is not available on the API surface, consider usingHttpListeneror ASP.NETHttpContextto accept server WebSocket connections.
On .NET Core 2.1+, .NET 5+ or .NET Standard 2.1:
- To create a client or server
WebSocketfrom a stream, useWebSocket.CreateFromStream
Main Types
The main types provided by this library are:
- System.Net.WebSockets.WebSocketProtocol
Additional Documentation
- API reference can be found in: https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets.websocketprotocol
- Additional APIs mentioned:
- WebSocket static methods: https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets.websocket
- ClientWebSocket: https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket
- HttpListener context: https://learn.microsoft.com/en-us/dotnet/api/system.net.httplistenercontext
- ASP.NET HttpContext: https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcontext
License
System.Net.WebSockets.WebSocketProtocol is released as open source under the MIT license.
Downloads by Version
Depended On By
Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.