public virtual Task CloseAsync()
{
if (IsClosedOrClosedSent(WebSocket))
{
return TaskAsyncHelper.Empty;
}
var closeContext = new CloseContext(this);
return _sendQueue.Enqueue(async state =>
{
var context = (CloseContext)state;
if (IsClosedOrClosedSent(context.Handler.WebSocket))
{
return;
}
try
{
await context.Handler.WebSocket
.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None)
.PreserveCulture();
}
catch (Exception ex)
{
// Swallow exceptions on close
_logger.LogError("Error while closing the websocket: " + ex);
}
},
closeContext);
}