ClientWebSocket.ConnectAsync Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
ConnectAsync(Uri, CancellationToken) |
Connects to a WebSocket server as an asynchronous operation. |
ConnectAsync(Uri, HttpMessageInvoker, CancellationToken) |
Connects to a WebSocket server as an asynchronous operation. |
- Source:
- ClientWebSocket.cs
- Source:
- ClientWebSocket.cs
- Source:
- ClientWebSocket.cs
Connects to a WebSocket server as an asynchronous operation.
public:
System::Threading::Tasks::Task ^ ConnectAsync(Uri ^ uri, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ConnectAsync (Uri uri, System.Threading.CancellationToken cancellationToken);
member this.ConnectAsync : Uri * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ConnectAsync (uri As Uri, cancellationToken As CancellationToken) As Task
Parameters
- uri
- Uri
The URI of the WebSocket server to connect to.
- cancellationToken
- CancellationToken
A cancellation token used to propagate notification that the operation should be canceled.
Returns
The task object representing the asynchronous operation.
Exceptions
The cancellation token was canceled. This exception is stored into the returned task.
Remarks
The ConnectAsync method initiates the WebSocket handshake with a remote network destination, and then negotiates the sub-protocol.
For plain WebSocket connections, use the ws://
scheme in the uri. For secure WebSocket connections over TLS/SSL, use the wss://
scheme.
This operation will not block. The returned Task object will complete after the connect request on the ClientWebSocket instance has completed.
The WebSocket handshake request has default headers for HTTP/1.1 request:
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Key: [generated key]
For HTTP/2 request:
:protocol: websocket
By default, HTTP/1.1 will be used. To change HTTP version used or enable HTTP version upgrade/downgrade, see HttpVersion option and HttpVersionPolicy option.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- ClientWebSocket.cs
- Source:
- ClientWebSocket.cs
- Source:
- ClientWebSocket.cs
Connects to a WebSocket server as an asynchronous operation.
public:
System::Threading::Tasks::Task ^ ConnectAsync(Uri ^ uri, System::Net::Http::HttpMessageInvoker ^ invoker, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ConnectAsync (Uri uri, System.Net.Http.HttpMessageInvoker? invoker, System.Threading.CancellationToken cancellationToken);
member this.ConnectAsync : Uri * System.Net.Http.HttpMessageInvoker * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ConnectAsync (uri As Uri, invoker As HttpMessageInvoker, cancellationToken As CancellationToken) As Task
Parameters
- uri
- Uri
The URI of the WebSocket server to connect to.
- invoker
- HttpMessageInvoker
The HttpMessageInvoker instance to use for connecting.
- cancellationToken
- CancellationToken
A cancellation token used to propagate notification that the operation should be canceled.
Returns
The task object representing the asynchronous operation.
Exceptions
The cancellation token was canceled. This exception is stored into the returned task.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 7, 8, 9 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: