/// <summary> /// Closes the connection as an asynchronous operation using the close handshake defined by the underlying implementation. /// </summary> /// <param name="closeStatus">Indicates the reason for closing the connection.</param> /// <param name="statusDescription">Specifies a human readable explanation as to why the connection is closed.</param> /// <param name="cancellationToken">The token that can be used to propagate notification that operations should be /// canceled.</param> /// <returns>Task.</returns> public Task CloseAsync(ClientConnectionCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken) { _connectionClosed = true; _connectionClosedByServer = true; this.CloseStatusDescription = statusDescription; this.CloseStatus = closeStatus; this.State = ClientConnectionState.Closed; return(Task.CompletedTask); }
/// <summary> /// Instructs the client proxy to close its connection from the server side, no additional messages will be sent to it. /// </summary> /// <param name="reason">The status reason why the connection is being closed. This may be /// sent to the client depending on implementation.</param> /// <param name="message">A human readonable description as to why the connection was closed by /// the server.</param> /// <param name="cancelToken">A cancellation token.</param> /// <returns>Task.</returns> public Task CloseConnection( ClientConnectionCloseStatus reason, string message = null, CancellationToken cancelToken = default) { this.ProcessCloseRequest(); if (_connection.State == ClientConnectionState.Open) { return(_connection.CloseAsync(reason, message, cancelToken)); } return(Task.CompletedTask); }
/// <summary> /// Closes the connection as an asynchronous operation using the close handshake defined by the underlying implementation. /// </summary> /// <param name="closeStatus">Indicates the reason for closing the connection.</param> /// <param name="statusDescription">Specifies a human readable explanation as to why the connection is closed.</param> /// <param name="cancellationToken">The token that can be used to propagate notification that operations should be /// canceled.</param> /// <returns>Task.</returns> public async Task CloseAsync(ClientConnectionCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken) { try { await _webSocket.CloseAsync( closeStatus.ToWebSocketCloseStatus(), statusDescription, cancellationToken) .ConfigureAwait(false); } catch (WebSocketException wse) { if (wse.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { // ignore } else { throw; } } }
public void ToClientConnectionCloseStatus(ClientConnectionCloseStatus socketStatus, WebSocketCloseStatus expectedCloseStatus) { Assert.AreEqual(expectedCloseStatus, socketStatus.ToWebSocketCloseStatus()); }
/// <summary> /// Converts the internal close status to its websocket specific status. /// </summary> /// <param name="closeStatus">The internal close status to convert.</param> /// <returns>System.Nullable<WebSocketCloseStatus>.</returns> public static WebSocketCloseStatus ToWebSocketCloseStatus(this ClientConnectionCloseStatus closeStatus) { return((WebSocketCloseStatus)(int)closeStatus); }