From d9a5e014324194ae3011e40e63d5bf0c34752816 Mon Sep 17 00:00:00 2001 From: Brennan Date: Thu, 12 Nov 2020 13:02:23 -0800 Subject: [PATCH] Support multi-buffer sends in WebAssembly WebSockets Proposed fix for https://github.com/dotnet/runtime/issues/44551 --- .../BrowserWebSockets/BrowserWebSocket.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs index c07cd8f01e793..706567d32049d 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs +++ b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs @@ -357,14 +357,21 @@ public override Task SendAsync(ArraySegment buffer, WebSocketMessageType m WebSocketValidate.ValidateArraySegment(buffer, nameof(buffer)); - _writeBuffer ??= new MemoryStream(); - _writeBuffer.Write(buffer.Array!, buffer.Offset, buffer.Count); - if (!endOfMessage) + { + _writeBuffer ??= new MemoryStream(); + _writeBuffer.Write(buffer.Array!, buffer.Offset, buffer.Count); return Task.CompletedTask; + } MemoryStream writtenBuffer = _writeBuffer; _writeBuffer = null; + + if (writtenBuffer is not null) + { + writtenBuffer.Write(buffer.Array!, buffer.Offset, buffer.Count); + } + buffer = writtenBuffer?.ToArray() ?? buffer; try {