Skip to content

Commit

Permalink
fix: bugfix to send KeepAlive messages as text rather than binary
Browse files Browse the repository at this point in the history
  • Loading branch information
jjmaldonis committed Sep 8, 2023
1 parent 460a300 commit a1cf136
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Deepgram/Clients/LiveTranscriptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/// <summary>
Expand All @@ -185,7 +185,7 @@ public void KeepAlive()
/// <param name="data">The data to be sent over the websocket.</param>
public virtual void SendData(byte[] data)
{
EnqueueForSending(new MessageToSend(data));
EnqueueForSending(new MessageToSend(data, WebSocketMessageType.Binary));
}

private Uri GetWSSUriWithQuerystring(string uriSegment, LiveTranscriptionOptions queryParameters) =>
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -311,7 +311,7 @@ private void EnqueueForSending(MessageToSend message)
}
}

private async Task Send(ArraySegment<byte> data, CancellationToken token)
private async Task Send(MessageToSend message, CancellationToken token)
{
if (_clientWebSocket.State != WebSocketState.Open)
{
Expand All @@ -320,7 +320,7 @@ private async Task Send(ArraySegment<byte> 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);
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions Deepgram/Models/MessageToSend.cs
Original file line number Diff line number Diff line change
@@ -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<byte>(message);
MessageType = type;
}

public ArraySegment<byte> Message { get; }
public readonly ArraySegment<byte> Message { get; }

Check failure on line 14 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

Check failure on line 14 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

Check failure on line 14 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / test (6.0.x)

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

Check failure on line 14 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

public readonly WebSocketMessageType MessageType { get; }

Check failure on line 16 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

Check failure on line 16 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / build

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

Check failure on line 16 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / test (6.0.x)

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.

Check failure on line 16 in Deepgram/Models/MessageToSend.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

Feature 'readonly members' is not available in C# 7.3. Please use language version 8.0 or greater.
}
}

0 comments on commit a1cf136

Please sign in to comment.