AspNetWebSocket::CloseAsync Method (WebSocketCloseStatus, String^, CancellationToken)
Sends an asynchronous message to a client to close the connection. If the server initiates the request to close the connection, the method waits for the client to acknowledge the request before it returns.
Assembly: System.Web (in System.Web.dll)
public: virtual Task^ CloseAsync( WebSocketCloseStatus closeStatus, String^ statusDescription, CancellationToken cancellationToken ) override
Parameters
- closeStatus
-
Type:
System.Net.WebSockets::WebSocketCloseStatus
The status code of the close operation.
- statusDescription
-
Type:
System::String^
The status message of the close operation.
- cancellationToken
-
Type:
System.Threading::CancellationToken
The object that cancels a pending operation.
| Exception | Condition |
|---|---|
| ObjectDisposedException | The AspNetWebSocket object was previously disposed. |
| System::InvalidOperationException | The AspNetWebSocket object is in an aborted state. -or- Sending operations are unavailable. -or- Receiving operations are unavailable. |
You can use the CloseAsync and CloseOutputAsync methods for both client-initiated and server-initiated requests to close an AspNetWebSocket connection. The two methods handle client-initiated requests in the same way: After the client sends a message to the server to close the connection, the server calls one of these methods and sends an acknowledgment to the client, and then the method returns.
For server-initiated requests, the two methods work differently. The CloseAsync method sends a message to the client to close the connection, waits for a response, and then returns. The server does not wait for any additional data sent by the client. In contrast, the CloseOutputAsync method sends a message to the client to close the connection and returns without waiting for a response. After the method returns, you can call the ReceiveAsync method and handle either additional data or the acknowledgment that the client sends.
Available since 4.5