From 295a5b23a37182818f10c7b378a9806f0e5ff8ec Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Wed, 29 Nov 2023 14:57:59 +0100 Subject: [PATCH] Fixes issue with web sockets on Windows --- dev-proxy-plugins/Inspection/WebSocketServer.cs | 7 +++++++ 1 file changed, 7 insertions(+) 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(); } }