Skip to content

Commit dda2477

Browse files
Fixes issue with web sockets on Windows (#399)
1 parent a8a1860 commit dda2477

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

dev-proxy-plugins/Inspection/WebSocketServer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class WebSocketServer
1616
private HttpListener? listener;
1717
private int port;
1818
private WebSocket? webSocket;
19+
static SemaphoreSlim webSocketSemaphore = new SemaphoreSlim(1, 1);
1920

2021
public bool IsConnected => webSocket is not null;
2122
public event Action<string>? MessageReceived;
@@ -88,7 +89,13 @@ public async Task SendAsync<TMsg>(TMsg message)
8889
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
8990
});
9091

92+
// we need a semaphore to avoid multiple simultaneous writes
93+
// which aren't allowed
94+
await webSocketSemaphore.WaitAsync();
95+
9196
byte[] messageBytes = Encoding.UTF8.GetBytes(messageString);
9297
await webSocket.SendAsync(new ArraySegment<byte>(messageBytes), WebSocketMessageType.Text, true, CancellationToken.None);
98+
99+
webSocketSemaphore.Release();
93100
}
94101
}

0 commit comments

Comments
 (0)