/// <summary>
/// Skips the message queue and directly sends over the Websocket.
/// </summary>
/// <param name="bytes"></param>
/// <param name="ct"></param>
/// <returns></returns>
public async Task <SendResult> SendAsync(byte[] bytes, CancellationToken ct)
{
if (_ws.State != WebSocketState.Open)
{
TraceEvent(
$"(SendAsync) Invalid attempt to send while socket is not open",
EventIdentifiers.WS_ERR_SEND_SOCK_CLOSE,
TraceEventType.Error
);
throw new InvalidOperationException("Websocket is not open");
}
TraceEvent($"(SendAsync) Queued bytes [ len: {bytes.Length} ]", EventIdentifiers.WS_VER_QMSG_ASYNC, TraceEventType.Verbose);
ArraySegment <byte> buf = new ArraySegment <byte>(bytes);
await _sem.WaitAsync(ct);
if (ct.IsCancellationRequested)
{
_sem.Release();
TraceEvent($"(SendAsync) Operation cancelled", EventIdentifiers.WS_WAR_CANC_SEND, TraceEventType.Warning);
ct.ThrowIfCancellationRequested();
}
await _ws.SendAsync(new ArraySegment <byte>(bytes, 0, bytes.Length), _msgType, true, ct);
return(SendResult.Sent);
}