@@ -22,6 +22,20 @@ public ConnectionManager(ILoggerFactory loggerFactory)
2222 Connections = new ConcurrentDictionary < string , WebSocketTransport > ( ) ;
2323 }
2424
25+ private void PrepareBytes ( ref byte [ ] bytes , JsonObject properties )
26+ {
27+ if ( bytes == null )
28+ {
29+ throw new ArgumentNullException ( nameof ( bytes ) ) ;
30+ }
31+
32+ var props = JsonConvert . SerializeObject ( properties ) ;
33+ var propsBytes = Encoding . UTF8 . GetBytes ( $ "{ SocketsConstants . Splitter } { props } ") ;
34+
35+ var bytesCount = bytes . Length ;
36+ bytes = bytes . Concat ( propsBytes ) . ToArray ( ) ;
37+ }
38+
2539 private async Task SendAsync ( WebSocketTransport transport , WebSocketMessageDescriptor descriptor )
2640 {
2741 if ( descriptor == null )
@@ -40,6 +54,21 @@ await transport.WebSocket.SendAsync(descriptor.Segments,
4054 CancellationToken . None ) ;
4155 }
4256
57+ private async Task SendBinaryAsync ( WebSocketTransport transport , byte [ ] chunkedBytes , bool endOfMessage )
58+ {
59+ if ( transport == null )
60+ {
61+ throw new ArgumentNullException ( nameof ( transport ) ) ;
62+ }
63+
64+ var segments = new ArraySegment < byte > ( chunkedBytes ) ;
65+
66+ await transport . WebSocket . SendAsync ( segments ,
67+ WebSocketMessageType . Binary ,
68+ endOfMessage ,
69+ CancellationToken . None ) ;
70+ }
71+
4372 public async Task BroadcastAsync ( WebSocketMessageContext context )
4473 {
4574 if ( context == null )
@@ -66,6 +95,37 @@ public async Task BroadcastAsync(WebSocketMessageContext context)
6695 }
6796 }
6897
98+ public async Task BroadcastBinaryAsync ( byte [ ] bytes , JsonObject properties )
99+ {
100+ PrepareBytes ( ref bytes , properties ) ;
101+
102+ var buffer = new byte [ SocketsConstants . ChunkSize ] ;
103+ using ( var ms = new MemoryStream ( bytes ) )
104+ {
105+ using ( BinaryReader br = new BinaryReader ( ms ) )
106+ {
107+ byte [ ] chunkedBytes = null ;
108+ do
109+ {
110+ chunkedBytes = br . ReadBytes ( SocketsConstants . ChunkSize ) ;
111+ var endOfMessage = false ;
112+
113+ if ( chunkedBytes . Length < SocketsConstants . ChunkSize )
114+ endOfMessage = true ;
115+
116+ foreach ( var connection in Connections )
117+ {
118+ await SendBinaryAsync ( transport : connection . Value , chunkedBytes : chunkedBytes , endOfMessage : endOfMessage ) ;
119+ }
120+
121+ if ( endOfMessage )
122+ break ;
123+
124+ } while ( chunkedBytes . Length <= SocketsConstants . ChunkSize ) ;
125+ }
126+ }
127+ }
128+
69129 public async Task SendAsync ( string connectionId , WebSocketMessageContext context )
70130 {
71131 WebSocketTransport transport = null ;
@@ -93,11 +153,7 @@ public async Task SendBinaryAsync(string connectionId, byte[] bytes, JsonObject
93153 throw new ArgumentOutOfRangeException ( nameof ( transport ) ) ;
94154 }
95155
96- var props = JsonConvert . SerializeObject ( properties ) ;
97- var propsBytes = Encoding . UTF8 . GetBytes ( $ "{ SocketsConstants . Splitter } { props } ") ;
98-
99- var bytesCount = bytes . Length ;
100- bytes = bytes . Concat ( propsBytes ) . ToArray ( ) ;
156+ PrepareBytes ( ref bytes , properties ) ;
101157
102158 var buffer = new byte [ SocketsConstants . ChunkSize ] ;
103159 using ( var ms = new MemoryStream ( bytes ) )
0 commit comments