Skip to content

Commit

Permalink
Fixes issue with web sockets on Windows (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored Nov 29, 2023
1 parent a8a1860 commit dda2477
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dev-proxy-plugins/Inspection/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>? MessageReceived;
Expand Down Expand Up @@ -88,7 +89,13 @@ public async Task SendAsync<TMsg>(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<byte>(messageBytes), WebSocketMessageType.Text, true, CancellationToken.None);

webSocketSemaphore.Release();
}
}

0 comments on commit dda2477

Please sign in to comment.