Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,39 @@ public override void Abort()
Dispose();
}

public override async Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
{
_writeBuffer = null;
ThrowIfNotConnected();
await CloseAsyncCore(closeStatus, statusDescription, cancellationToken).ConfigureAwait(continueOnCapturedContext: true);
}

private async Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
{
ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent);

WebSocketValidate.ValidateCloseStatus(closeStatus, statusDescription);

_tcsClose = new TaskCompletionSource();
_innerWebSocketCloseStatus = closeStatus;
_innerWebSocketCloseStatusDescription = statusDescription;
_innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription);
await _tcsClose.Task.ConfigureAwait(continueOnCapturedContext: true);
try
{
ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent);
}
catch (Exception exc)
{
return Task.FromException(exc);
}

return CloseAsyncCore(closeStatus, statusDescription, cancellationToken);
}

private Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
{
try
{
_tcsClose = new TaskCompletionSource();
_innerWebSocketCloseStatus = closeStatus;
_innerWebSocketCloseStatusDescription = statusDescription;
_innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription);
return _tcsClose.Task;
}
catch (Exception exc)
{
return Task.FromException(exc);
}
}

public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) => throw new PlatformNotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public async Task CloseAsync_CloseDescriptionIsMaxLength_Success(Uri server)

[OuterLoop("Uses external server")]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/45531", TestPlatforms.Browser)]
public async Task CloseAsync_CloseDescriptionIsMaxLengthPlusOne_ThrowsArgumentException(Uri server)
{
string closeDescription = new string('C', CloseDescriptionMaxLength + 1);
Expand Down