@@ -39,7 +39,7 @@ public ConnectionManager(IStreamCompressor compressor,
3939 Connections = new ConcurrentDictionary < string , WebSocketTransport > ( ) ;
4040 }
4141
42- private async Task < byte [ ] > PrepareBytesAsync ( byte [ ] input , IDictionary < string , object > properties , bool compression = true )
42+ private async Task < byte [ ] > PrepareBytesAsync ( byte [ ] input , IDictionary < string , object > properties )
4343 {
4444 if ( input == null )
4545 {
@@ -49,19 +49,23 @@ private async Task<byte[]> PrepareBytesAsync(byte[] input, IDictionary<string, o
4949 if ( properties == null )
5050 properties = new Dictionary < string , object > ( ) ;
5151
52- object compressedKey = null ;
53- if ( properties . TryGetValue ( CompressedKey , out compressedKey ) )
54- properties [ CompressedKey ] = compression ;
52+ bool compressed = GZipHelper . IsGZipHeader ( input ) ;
53+
54+ object key = null ;
55+ if ( properties . TryGetValue ( CompressedKey , out key ) )
56+ properties [ CompressedKey ] = compressed ;
5557 else
56- properties . Add ( CompressedKey , compression ) ;
58+ properties . Add ( CompressedKey , compressed ) ;
59+
60+ string props = JsonConvert . SerializeObject ( properties ) ;
61+ byte [ ] header = Encoding . UTF8 . GetBytes ( $ "{ props } { Splitter } ") ;
5762
58- var props = JsonConvert . SerializeObject ( properties ) ;
59- var propsBytes = Encoding . UTF8 . GetBytes ( $ " { props } { Splitter } " ) ;
63+ if ( compressed )
64+ header = await _compressor . CompressAsync ( header ) ;
6065
61- var bytesCount = input . Length ;
62- input = propsBytes . Concat ( input ) . ToArray ( ) ;
66+ input = header . Concat ( input ) . ToArray ( ) ;
6367
64- if ( compression )
68+ if ( ! compressed )
6569 return await _compressor . CompressAsync ( input ) ;
6670
6771 return input ;
@@ -156,14 +160,14 @@ public async Task BroadcastAsync(WebSocketMessageContext context)
156160 }
157161 }
158162
159- public async Task BroadcastBinaryAsync ( byte [ ] inputs , IDictionary < string , object > properties , bool compression = true )
163+ public async Task BroadcastBinaryAsync ( byte [ ] inputs , IDictionary < string , object > properties )
160164 {
161165 if ( ! Connections . Any ( ) )
162166 {
163167 return ;
164168 }
165169
166- var bytes = await PrepareBytesAsync ( inputs , properties , compression ) ;
170+ var bytes = await PrepareBytesAsync ( inputs , properties ) ;
167171
168172 using ( var ms = new MemoryStream ( bytes ) )
169173 {
@@ -215,7 +219,7 @@ public async Task SendAsync(string connectionId, WebSocketMessageContext context
215219 await SendAsync ( transport , descriptor ) ;
216220 }
217221
218- public async Task SendBinaryAsync ( string connectionId , byte [ ] input , IDictionary < string , object > properties , bool compression = true )
222+ public async Task SendBinaryAsync ( string connectionId , byte [ ] input , IDictionary < string , object > properties )
219223 {
220224 if ( ! Connections . Any ( ) )
221225 {
@@ -228,7 +232,7 @@ public async Task SendBinaryAsync(string connectionId, byte[] input, IDictionary
228232 throw new ArgumentOutOfRangeException ( nameof ( transport ) ) ;
229233 }
230234
231- byte [ ] bytes = await PrepareBytesAsync ( input , properties , compression ) ;
235+ byte [ ] bytes = await PrepareBytesAsync ( input , properties ) ;
232236
233237 using ( var ms = new MemoryStream ( bytes ) )
234238 {
0 commit comments