@@ -56,6 +56,7 @@ protected StreamingRpcClient(string url, object logger, IWebSocket socket = defa
5656 _logger = logger ;
5757 _sem = new SemaphoreSlim ( 1 , 1 ) ;
5858 _connectionStats = new ConnectionStats ( ) ;
59+ ClientSocket . ConnectionStateChangedEvent += ( sender , state ) => ConnectionStateChangedEvent ? . Invoke ( sender , state ) ;
5960 }
6061
6162 /// <summary>
@@ -70,7 +71,7 @@ public async Task ConnectAsync()
7071 if ( ClientSocket . State != WebSocketState . Open )
7172 {
7273 await ClientSocket . ConnectAsync ( NodeAddress , CancellationToken . None ) ;
73- _ = StartListening ( ) ;
74+ ClientSocket . OnMessage += DispatchMessage ;
7475 ConnectionStateChangedEvent ? . Invoke ( this , State ) ;
7576 }
7677 }
@@ -80,6 +81,16 @@ public async Task ConnectAsync()
8081 }
8182 }
8283
84+ private void DispatchMessage ( byte [ ] message )
85+ {
86+ HandleNewMessage ( new Memory < byte > ( message ) ) ;
87+ _connectionStats . AddReceived ( ( uint ) message . Length ) ;
88+ if ( ClientSocket . State != WebSocketState . Open && ClientSocket . State != WebSocketState . Connecting )
89+ {
90+ ConnectionStateChangedEvent ? . Invoke ( this , State ) ;
91+ }
92+ }
93+
8394 /// <inheritdoc cref="IStreamingRpcClient.DisconnectAsync"/>
8495 public async Task DisconnectAsync ( )
8596 {
@@ -94,6 +105,7 @@ public async Task DisconnectAsync()
94105 //and will also notify when there is a non-user triggered disconnection event
95106
96107 // handle disconnection cleanup
108+ ClientSocket . OnMessage -= DispatchMessage ;
97109 ClientSocket . Dispose ( ) ;
98110 ClientSocket = new WebSocketWrapper ( ) ;
99111 CleanupSubscriptions ( ) ;
@@ -105,78 +117,6 @@ public async Task DisconnectAsync()
105117 }
106118 }
107119
108- /// <summary>
109- /// Starts listeing to new messages.
110- /// </summary>
111- /// <returns>Returns the task representing the asynchronous task.</returns>
112- private async Task StartListening ( )
113- {
114- while ( ClientSocket . State is WebSocketState . Open or WebSocketState . Connecting )
115- {
116- try
117- {
118- await ReadNextMessage ( ) ;
119- }
120- catch ( Exception e )
121- {
122- if ( _logger != null )
123- {
124- Console . WriteLine ( $ "Exception trying to read next message: { e . Message } ") ;
125- }
126- }
127- }
128-
129- if ( _logger != null )
130- {
131- Console . WriteLine ( $ "Stopped reading messages. ClientSocket.State changed to { ClientSocket . State } ") ;
132- }
133- ConnectionStateChangedEvent ? . Invoke ( this , State ) ;
134- }
135-
136- /// <summary>
137- /// Reads the next message from the socket.
138- /// </summary>
139- /// <param name="cancellationToken">The cancelation token.</param>
140- /// <returns>Returns the task representing the asynchronous task.</returns>
141- private async Task ReadNextMessage ( CancellationToken cancellationToken = default )
142- {
143- var buffer = new byte [ 32768 ] ;
144- Memory < byte > mem = new ( buffer ) ;
145- WebSocketReceiveResult result = await ClientSocket . ReceiveAsync ( mem , cancellationToken ) ;
146- int count = result . Count ;
147-
148- if ( result . MessageType == WebSocketMessageType . Close )
149- {
150- await ClientSocket . CloseAsync ( WebSocketCloseStatus . NormalClosure , string . Empty , cancellationToken ) ;
151- }
152- else
153- {
154- if ( ! result . EndOfMessage )
155- {
156- MemoryStream ms = new MemoryStream ( ) ;
157- ms . Write ( mem . Span . ToArray ( ) , 0 , mem . Span . Length ) ;
158-
159-
160- while ( ! result . EndOfMessage )
161- {
162- result = await ClientSocket . ReceiveAsync ( mem , cancellationToken ) . ConfigureAwait ( false ) ;
163-
164- var memSlice = mem . Slice ( 0 , result . Count ) . Span . ToArray ( ) ;
165- ms . Write ( memSlice , 0 , memSlice . Length ) ;
166- count += result . Count ;
167- }
168-
169- mem = new Memory < byte > ( ms . ToArray ( ) ) ;
170- }
171- else
172- {
173- mem = mem . Slice ( 0 , count ) ;
174- }
175- _connectionStats . AddReceived ( ( uint ) count ) ;
176- HandleNewMessage ( mem ) ;
177- }
178- }
179-
180120 /// <summary>
181121 /// Handless a new message payload.
182122 /// </summary>
0 commit comments