66using System ;
77using System . Collections . Concurrent ;
88using System . Collections . Generic ;
9+ using System . Diagnostics ;
910using System . IO ;
1011using System . Linq ;
1112using System . Net . WebSockets ;
@@ -39,17 +40,17 @@ public ConnectionManager(IStreamCompressor compressor,
3940 Connections = new ConcurrentDictionary < string , WebSocketTransport > ( ) ;
4041 }
4142
42- private async Task < byte [ ] > PrepareBytesAsync ( byte [ ] input , IDictionary < string , object > properties )
43+ private async Task < byte [ ] > PrepareBytesAsync ( byte [ ] body , IDictionary < string , object > properties )
4344 {
44- if ( input == null )
45+ if ( body == null )
4546 {
46- throw new ArgumentNullException ( nameof ( input ) ) ;
47+ throw new ArgumentNullException ( nameof ( body ) ) ;
4748 }
4849
4950 if ( properties == null )
5051 properties = new Dictionary < string , object > ( ) ;
5152
52- bool compressed = GZipHelper . IsGZipHeader ( input ) ;
53+ bool compressed = GZipHelper . IsGZipBody ( body ) ;
5354
5455 object key = null ;
5556 if ( properties . TryGetValue ( CompressedKey , out key ) )
@@ -58,17 +59,22 @@ private async Task<byte[]> PrepareBytesAsync(byte[] input, IDictionary<string, o
5859 properties . Add ( CompressedKey , compressed ) ;
5960
6061 string props = JsonConvert . SerializeObject ( properties ) ;
61- byte [ ] header = Encoding . UTF8 . GetBytes ( $ "{ props } { Splitter } ") ;
62+ byte [ ] header = Encoding . UTF8 . GetBytes ( $ "{ props } ") ;
6263
63- if ( compressed )
64- header = await _compressor . CompressAsync ( header ) ;
65-
66- input = header . Concat ( input ) . ToArray ( ) ;
64+ #if DEBUG
65+ if ( properties . TryGetValue ( "Key" , out key ) )
66+ {
67+ int length = body . Length ;
68+ Debug . WriteLine ( $ "=====Key: { key ? . ToString ( ) } =====Length: { length } =====") ;
69+ }
70+ #endif
6771
6872 if ( ! compressed )
69- return await _compressor . CompressAsync ( input ) ;
73+ body = await _compressor . CompressAsync ( body ) ;
74+
75+ body = header . Concat ( Splitter ) . Concat ( body ) . ToArray ( ) ;
7076
71- return input ;
77+ return body ;
7278 }
7379
7480 private async Task SendAsync ( WebSocketTransport transport , WebSocketMessageDescriptor descriptor )
@@ -111,7 +117,7 @@ public async Task ConnectAsync(WebSocket webSocket)
111117 var context = new WebSocketMessageContext ( ) ;
112118 context . Command = WebSocketCommands . Handshake ;
113119 context . Value = connectionId ;
114- context . State = await _initState . GetStateAsync ( ) ;
120+ context . Header = await _initState . GetStateAsync ( ) ;
115121 Connections . TryAdd ( connectionId , transport ) ;
116122
117123 await SendAsync ( connectionId , context ) ;
0 commit comments