@@ -11,33 +11,43 @@ public sealed class ClientWebSocketManaged : IClientWebSocket
1111 public ClientWebSocketManaged ( )
1212 {
1313 _ws = new System . Net . WebSockets . Managed . ClientWebSocket ( ) ;
14+ _sendLock = new SemaphoreSlim ( 1 , 1 ) ;
1415 }
1516
1617 readonly System . Net . WebSockets . Managed . ClientWebSocket _ws ;
18+ readonly SemaphoreSlim _sendLock ;
1719
1820 public Action < object > ConfigOptions { get ; set ; }
1921
2022 public WebSocketState State => _ws . State ;
2123
2224 public async Task CloseAsync ( WebSocketCloseStatus closeStatus , string statusDescription , CancellationToken cancellationToken )
2325 {
24- await _ws . CloseAsync ( closeStatus , statusDescription , cancellationToken ) ;
26+ await _ws . CloseAsync ( closeStatus , statusDescription , cancellationToken ) . ConfigureAwait ( false ) ;
2527 }
2628
2729 public async Task ConnectAsync ( Uri uri , CancellationToken cancellationToken )
2830 {
2931 ConfigOptions ? . Invoke ( _ws . Options ) ;
30- await _ws . ConnectAsync ( uri , cancellationToken ) ;
32+ await _ws . ConnectAsync ( uri , cancellationToken ) . ConfigureAwait ( false ) ;
3133 }
3234
3335 public async Task < WebSocketReceiveResult > ReceiveAsync ( ArraySegment < byte > buffer , CancellationToken cancellationToken )
3436 {
35- return await _ws . ReceiveAsync ( buffer , cancellationToken ) ;
37+ return await _ws . ReceiveAsync ( buffer , cancellationToken ) . ConfigureAwait ( false ) ;
3638 }
3739
3840 public async Task SendAsync ( ArraySegment < byte > buffer , WebSocketMessageType messageType , bool endOfMessage , CancellationToken cancellationToken )
3941 {
40- await _ws . SendAsync ( buffer , messageType , endOfMessage , cancellationToken ) ;
42+ try
43+ {
44+ await _sendLock . WaitAsync ( ) . ConfigureAwait ( false ) ;
45+ await _ws . SendAsync ( buffer , messageType , endOfMessage , cancellationToken ) . ConfigureAwait ( false ) ;
46+ }
47+ finally
48+ {
49+ _sendLock . Release ( ) ;
50+ }
4151 }
4252
4353 public void Dispose ( )
0 commit comments