diff --git a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs index 81c37cd75d7..d08a651a40b 100644 --- a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs +++ b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs @@ -21,6 +21,7 @@ // --------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Net.WebSockets; @@ -57,7 +58,8 @@ internal class WebSocketImplementation : WebSocket const int MAX_PING_PONG_PAYLOAD_LEN = 125; WebSocketCloseStatus? _closeStatus; string _closeStatusDescription; - SemaphoreSlim sendSemaphore = new SemaphoreSlim(1, 1); + bool sendingMessage = false; + Queue> messagesToSend = new Queue>(); public event EventHandler Pong; @@ -223,14 +225,15 @@ public override async Task SendAsync(ArraySegment buffer, WebSocketMessage // In SslStream, only one SendAsync can be going at a time // if Send is called multiple time, only the first one calls SendAsync, // the other ones queue up the message - await sendSemaphore.WaitAsync(); - try - { - await SendAsyncInternal(buffer, messageType, endOfMessage, cancellationToken); - } - finally + messagesToSend.Enqueue(buffer); + if (!sendingMessage) { - sendSemaphore.Release(); + sendingMessage = true; + while (messagesToSend.Count > 0) + { + await SendAsyncInternal(messagesToSend.Dequeue(), messageType, endOfMessage, cancellationToken); + } + sendingMessage = false; } }