diff --git a/dev-proxy-plugins/Inspection/WebSocketServer.cs b/dev-proxy-plugins/Inspection/WebSocketServer.cs index 07bbec74..354ab916 100644 --- a/dev-proxy-plugins/Inspection/WebSocketServer.cs +++ b/dev-proxy-plugins/Inspection/WebSocketServer.cs @@ -16,6 +16,7 @@ public class WebSocketServer private HttpListener? listener; private int port; private WebSocket? webSocket; + static SemaphoreSlim webSocketSemaphore = new SemaphoreSlim(1, 1); public bool IsConnected => webSocket is not null; public event Action? MessageReceived; @@ -88,7 +89,13 @@ public async Task SendAsync(TMsg message) DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); + // we need a semaphore to avoid multiple simultaneous writes + // which aren't allowed + await webSocketSemaphore.WaitAsync(); + byte[] messageBytes = Encoding.UTF8.GetBytes(messageString); await webSocket.SendAsync(new ArraySegment(messageBytes), WebSocketMessageType.Text, true, CancellationToken.None); + + webSocketSemaphore.Release(); } }