Skip to content

Commit

Permalink
Merge pull request #337 from dvonthenen/improve-text-buffer-msg
Browse files Browse the repository at this point in the history
Improve WebSocketMessage Buffer Length Handling
  • Loading branch information
davidvonthenen authored Sep 27, 2024
2 parents bfcffd0 + 547e9cc commit 7125208
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 2 additions & 3 deletions Deepgram/Clients/Listen/v1/WebSocket/WebSocketMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public WebSocketMessage(byte[] message, WebSocketMessageType type)

public WebSocketMessage(byte[] message, WebSocketMessageType type, int length)
{
if (length != Constants.UseArrayLengthForSend || length < Constants.UseArrayLengthForSend)
if (length != Constants.UseArrayLengthForSend && length <= message.Length && length > 0)
{
Message = new ArraySegment<byte>(message, 0, length);
}
Expand All @@ -22,10 +22,9 @@ public WebSocketMessage(byte[] message, WebSocketMessageType type, int length)
Message = new ArraySegment<byte>(message, 0, message.Length);
}
MessageType = type;
Length = length;
}

public int Length { get; }
public int Length => Message.Count;

public ArraySegment<byte> Message { get; }

Expand Down
5 changes: 2 additions & 3 deletions Deepgram/Clients/Speak/v1/WebSocket/WebSocketMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public WebSocketMessage(byte[] message, WebSocketMessageType type)

public WebSocketMessage(byte[] message, WebSocketMessageType type, int length)
{
if (length != Constants.UseArrayLengthForSend || length < Constants.UseArrayLengthForSend)
if (length != Constants.UseArrayLengthForSend && length <= message.Length && length > 0)
{
Message = new ArraySegment<byte>(message, 0, length);
}
Expand All @@ -22,10 +22,9 @@ public WebSocketMessage(byte[] message, WebSocketMessageType type, int length)
Message = new ArraySegment<byte>(message, 0, message.Length);
}
MessageType = type;
Length = length;
}

public int Length { get; }
public int Length => Message.Count;

public ArraySegment<byte> Message { get; }

Expand Down

0 comments on commit 7125208

Please sign in to comment.