From a1cf136b6ed2d33745a5d25d5c9f6ad7134a045c Mon Sep 17 00:00:00 2001 From: Jason Maldonis Date: Fri, 8 Sep 2023 13:43:29 -0500 Subject: [PATCH] fix: bugfix to send KeepAlive messages as text rather than binary --- Deepgram/Clients/LiveTranscriptionClient.cs | 10 +++++----- Deepgram/Models/MessageToSend.cs | 8 ++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Deepgram/Clients/LiveTranscriptionClient.cs b/Deepgram/Clients/LiveTranscriptionClient.cs index 087164e5..419352a8 100644 --- a/Deepgram/Clients/LiveTranscriptionClient.cs +++ b/Deepgram/Clients/LiveTranscriptionClient.cs @@ -176,7 +176,7 @@ public void KeepAlive() var keepAliveMessage = JsonConvert.SerializeObject(new { type = "KeepAlive" }); var keepAliveBytes = Encoding.Default.GetBytes(keepAliveMessage); - SendData(keepAliveBytes); + EnqueueForSending(new MessageToSend(keepAliveBytes, WebSocketMessageType.Text)); } /// @@ -185,7 +185,7 @@ public void KeepAlive() /// The data to be sent over the websocket. public virtual void SendData(byte[] data) { - EnqueueForSending(new MessageToSend(data)); + EnqueueForSending(new MessageToSend(data, WebSocketMessageType.Binary)); } private Uri GetWSSUriWithQuerystring(string uriSegment, LiveTranscriptionOptions queryParameters) => @@ -212,7 +212,7 @@ private async Task ProcessSenderQueue() { while (_sendChannel.Reader.TryRead(out var message)) { - await Send(message.Message, _tokenSource.Token).ConfigureAwait(false); + await Send(message, _tokenSource.Token).ConfigureAwait(false); } } } @@ -311,7 +311,7 @@ private void EnqueueForSending(MessageToSend message) } } - private async Task Send(ArraySegment data, CancellationToken token) + private async Task Send(MessageToSend message, CancellationToken token) { if (_clientWebSocket.State != WebSocketState.Open) { @@ -320,7 +320,7 @@ private async Task Send(ArraySegment data, CancellationToken token) return; } - await _clientWebSocket.SendAsync(data, WebSocketMessageType.Binary, true, token).ConfigureAwait(false); + await _clientWebSocket.SendAsync(message.Message, message.MessageType, true, token).ConfigureAwait(false); } /// diff --git a/Deepgram/Models/MessageToSend.cs b/Deepgram/Models/MessageToSend.cs index da088f9f..3e85af40 100644 --- a/Deepgram/Models/MessageToSend.cs +++ b/Deepgram/Models/MessageToSend.cs @@ -1,14 +1,18 @@ using System; +using System.Net.WebSockets; namespace Deepgram.Models { internal readonly struct MessageToSend { - public MessageToSend(byte[] message) + public MessageToSend(byte[] message, WebSocketMessageType type) { Message = new ArraySegment(message); + MessageType = type; } - public ArraySegment Message { get; } + public readonly ArraySegment Message { get; } + + public readonly WebSocketMessageType MessageType { get; } } }