System.Net.WebSockets.WebSocketProtocol 5.1.0
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.
Showing the top 20 packages that depend on System.Net.WebSockets.WebSocketProtocol.
| Packages | Downloads |
|---|---|
|
Microsoft.AspNetCore.Http.Connections
Components for providing real-time bi-directional communication across the Web.
|
60 |
|
Microsoft.AspNetCore.WebSockets
ASP.NET Core web socket middleware for use on top of opaque servers.
|
60 |
.NET Framework 4.6.2
- System.Buffers (>= 4.6.0)
- System.Memory (>= 4.6.0)
- System.Numerics.Vectors (>= 4.6.0)
- System.Runtime.CompilerServices.Unsafe (>= 6.1.0)
- System.Threading.Tasks.Extensions (>= 4.6.0)
.NET 6.0
- No dependencies.
.NET Standard 2.0
- System.Buffers (>= 4.6.0)
- System.Memory (>= 4.6.0)
- System.Numerics.Vectors (>= 4.6.0)
- System.Runtime.CompilerServices.Unsafe (>= 6.1.0)
- System.Threading.Tasks.Extensions (>= 4.6.0)