|
1 | 1 | using Microsoft.Extensions.Logging; |
2 | 2 | using NetCoreStack.WebSockets.Internal; |
3 | | -using Newtonsoft.Json; |
4 | 3 | using System; |
5 | | -using System.Collections.Generic; |
6 | 4 | using System.IO; |
7 | 5 | using System.Net.WebSockets; |
8 | 6 | using System.Threading; |
@@ -32,95 +30,78 @@ public ClientWebSocketReceiver(IServiceProvider serviceProvider, |
32 | 30 |
|
33 | 31 | public async Task ReceiveAsync() |
34 | 32 | { |
35 | | - try |
| 33 | + var buffer = new byte[NCSConstants.ChunkSize]; |
| 34 | + var result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 35 | + while (!result.CloseStatus.HasValue) |
36 | 36 | { |
37 | | - var buffer = new byte[NCSConstants.ChunkSize]; |
38 | | - var result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
39 | | - while (!result.CloseStatus.HasValue) |
| 37 | + if (result.MessageType == WebSocketMessageType.Text) |
40 | 38 | { |
41 | | - if (result.MessageType == WebSocketMessageType.Text) |
| 39 | + byte[] inputs = null; |
| 40 | + using (var ms = new MemoryStream()) |
42 | 41 | { |
43 | | - try |
| 42 | + while (!result.EndOfMessage) |
44 | 43 | { |
45 | | - var context = result.ToContext(buffer); |
46 | | - if (context.Command == WebSocketCommands.Handshake) |
47 | | - { |
48 | | - _context.ConnectionId = context.Value?.ToString(); |
49 | | - _handshakeCallback?.Invoke(_context.ConnectionId); |
50 | | - } |
| 44 | + await ms.WriteAsync(buffer, 0, result.Count); |
| 45 | + result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 46 | + } |
51 | 47 |
|
52 | | - var invocator = _context.GetInvocator(_serviceProvider); |
53 | | - if (invocator != null) |
54 | | - { |
55 | | - await invocator.InvokeAsync(context); |
56 | | - } |
| 48 | + await ms.WriteAsync(buffer, 0, result.Count); |
| 49 | + inputs = ms.ToArray(); |
| 50 | + } |
| 51 | + try |
| 52 | + { |
| 53 | + var context = result.ToContext(inputs); |
| 54 | + if (context.Command == WebSocketCommands.Handshake) |
| 55 | + { |
| 56 | + _context.ConnectionId = context.Value?.ToString(); |
| 57 | + _handshakeCallback?.Invoke(_context.ConnectionId); |
57 | 58 | } |
58 | | - catch (Exception ex) |
| 59 | + var invocator = _context.GetInvocator(_serviceProvider); |
| 60 | + if (invocator != null) |
59 | 61 | { |
60 | | - _logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Text); |
| 62 | + await invocator.InvokeAsync(context); |
61 | 63 | } |
62 | | - result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
63 | 64 | } |
| 65 | + catch (Exception ex) |
| 66 | + { |
| 67 | + _logger.LogWarning(ex, "{0} An error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Text); |
| 68 | + } |
| 69 | + result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 70 | + } |
64 | 71 |
|
65 | | - if (result.MessageType == WebSocketMessageType.Binary) |
| 72 | + if (result.MessageType == WebSocketMessageType.Binary) |
| 73 | + { |
| 74 | + byte[] binaryResult = null; |
| 75 | + using (var ms = new MemoryStream()) |
66 | 76 | { |
67 | | - byte[] binaryResult = null; |
68 | | - using (var ms = new MemoryStream()) |
69 | | - { |
70 | | - while (!result.EndOfMessage) |
71 | | - { |
72 | | - if (!result.CloseStatus.HasValue) |
73 | | - { |
74 | | - await ms.WriteAsync(buffer, 0, result.Count); |
75 | | - } |
76 | | - result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
77 | | - } |
78 | | - if (result.EndOfMessage) |
79 | | - { |
80 | | - if (!result.CloseStatus.HasValue) |
81 | | - { |
82 | | - await ms.WriteAsync(buffer, 0, result.Count); |
83 | | - } |
84 | | - } |
85 | | - binaryResult = ms.ToArray(); |
86 | | - } |
87 | | - try |
| 77 | + while (!result.EndOfMessage) |
88 | 78 | { |
89 | | - var context = await result.ToBinaryContextAsync(_context.Compressor, binaryResult); |
90 | | - var invocator = _context.GetInvocator(_serviceProvider); |
91 | | - if (invocator != null) |
92 | | - { |
93 | | - await invocator.InvokeAsync(context); |
94 | | - } |
| 79 | + await ms.WriteAsync(buffer, 0, result.Count); |
| 80 | + result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
95 | 81 | } |
96 | | - catch (Exception ex) |
| 82 | + |
| 83 | + await ms.WriteAsync(buffer, 0, result.Count); |
| 84 | + binaryResult = ms.ToArray(); |
| 85 | + } |
| 86 | + try |
| 87 | + { |
| 88 | + var context = await result.ToBinaryContextAsync(_context.Compressor, binaryResult); |
| 89 | + var invocator = _context.GetInvocator(_serviceProvider); |
| 90 | + if (invocator != null) |
97 | 91 | { |
98 | | - _logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Binary); |
| 92 | + await invocator.InvokeAsync(context); |
99 | 93 | } |
100 | | - result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
101 | 94 | } |
| 95 | + catch (Exception ex) |
| 96 | + { |
| 97 | + _logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Binary); |
| 98 | + } |
| 99 | + result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
102 | 100 | } |
103 | | - |
104 | | - await _context.WebSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); |
105 | | - _closeCallback?.Invoke(_context); |
106 | 101 | } |
107 | | - catch (Exception ex) |
108 | | - { |
109 | | - var dictionary = new Dictionary<string, string>(); |
110 | | - dictionary.Add(nameof(_context.ConnectionId), _context.ConnectionId); |
111 | 102 |
|
112 | | - if (_context.InvocatorContext != null) |
113 | | - { |
114 | | - dictionary.Add(nameof(_context.InvocatorContext.ConnectorName), _context.InvocatorContext.ConnectorName); |
115 | | - dictionary.Add(nameof(_context.InvocatorContext.Uri), Convert.ToString(_context.InvocatorContext.Uri)); |
116 | | - } |
117 | | - |
118 | | - _logger.LogWarning(ex, "{0} receive exception: {1}", NCSConstants.WarningSymbol, JsonConvert.SerializeObject(dictionary)); |
119 | | - } |
120 | | - finally |
121 | | - { |
122 | | - _closeCallback?.Invoke(_context); |
123 | | - } |
| 103 | + await _context.WebSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); |
| 104 | + _closeCallback?.Invoke(_context); |
124 | 105 | } |
125 | 106 | } |
126 | 107 | } |
0 commit comments