11using System ;
2- using System . Collections . Generic ;
3- using System . Text ;
42using System . Threading ;
53using System . Threading . Tasks ;
64using Websocket . Client . Logging ;
@@ -13,8 +11,24 @@ public partial class WebsocketClient
1311 /// <summary>
1412 /// Force reconnection.
1513 /// Closes current websocket stream and perform a new connection to the server.
14+ /// In case of connection error it doesn't throw an exception, but tries to reconnect indefinitely.
1615 /// </summary>
17- public async Task Reconnect ( )
16+ public Task Reconnect ( )
17+ {
18+ return ReconnectInternal ( false ) ;
19+ }
20+
21+ /// <summary>
22+ /// Force reconnection.
23+ /// Closes current websocket stream and perform a new connection to the server.
24+ /// In case of connection error it throws an exception and doesn't perform any other reconnection try.
25+ /// </summary>
26+ public Task ReconnectOrFail ( )
27+ {
28+ return ReconnectInternal ( true ) ;
29+ }
30+
31+ private async Task ReconnectInternal ( bool failFast )
1832 {
1933 if ( ! IsStarted )
2034 {
@@ -24,39 +38,47 @@ public async Task Reconnect()
2438
2539 try
2640 {
27- await ReconnectSynchronized ( ReconnectionType . ByUser ) . ConfigureAwait ( false ) ;
28-
41+ await ReconnectSynchronized ( ReconnectionType . ByUser , failFast , null ) . ConfigureAwait ( false ) ;
2942 }
3043 finally
3144 {
3245 _reconnecting = false ;
3346 }
3447 }
3548
36-
37- private async Task ReconnectSynchronized ( ReconnectionType type )
49+ private async Task ReconnectSynchronized ( ReconnectionType type , bool failFast , Exception causedException )
3850 {
3951 using ( await _locker . LockAsync ( ) )
4052 {
41- await Reconnect ( type ) ;
53+ await Reconnect ( type , failFast , causedException ) ;
4254 }
4355 }
4456
45- private async Task Reconnect ( ReconnectionType type )
57+ private async Task Reconnect ( ReconnectionType type , bool failFast , Exception causedException )
4658 {
4759 IsRunning = false ;
4860 if ( _disposing )
4961 return ;
5062
5163 _reconnecting = true ;
52- if ( type != ReconnectionType . Error )
53- _disconnectedSubject . OnNext ( TranslateTypeToDisconnection ( type ) ) ;
5464
65+ var disType = TranslateTypeToDisconnection ( type ) ;
66+ var disInfo = DisconnectionInfo . Create ( disType , _client , causedException ) ;
67+ if ( type != ReconnectionType . Error )
68+ {
69+ _disconnectedSubject . OnNext ( disInfo ) ;
70+ if ( disInfo . CancelReconnection )
71+ {
72+ // reconnection canceled by user, do nothing
73+ Logger . Info ( L ( $ "Reconnecting canceled by user, exiting.") ) ;
74+ }
75+ }
76+
5577 _cancellation . Cancel ( ) ;
5678 _client ? . Abort ( ) ;
5779 _client ? . Dispose ( ) ;
5880
59- if ( ! IsReconnectionEnabled )
81+ if ( ! IsReconnectionEnabled || disInfo . CancelReconnection )
6082 {
6183 // reconnection disabled, do nothing
6284 IsStarted = false ;
@@ -66,7 +88,7 @@ private async Task Reconnect(ReconnectionType type)
6688
6789 Logger . Debug ( L ( "Reconnecting..." ) ) ;
6890 _cancellation = new CancellationTokenSource ( ) ;
69- await StartClient ( _url , _cancellation . Token , type ) . ConfigureAwait ( false ) ;
91+ await StartClient ( _url , _cancellation . Token , type , failFast ) . ConfigureAwait ( false ) ;
7092 _reconnecting = false ;
7193 }
7294
@@ -84,22 +106,22 @@ private void DeactivateLastChance()
84106
85107 private void LastChance ( object state )
86108 {
87- if ( ! IsReconnectionEnabled )
109+ if ( ! IsReconnectionEnabled || ReconnectTimeout == null )
88110 {
89111 // reconnection disabled, do nothing
90112 DeactivateLastChance ( ) ;
91113 return ;
92114 }
93115
94- var timeoutMs = Math . Abs ( ReconnectTimeoutMs ) ;
116+ var timeoutMs = Math . Abs ( ReconnectTimeout . Value . TotalMilliseconds ) ;
95117 var diffMs = Math . Abs ( DateTime . UtcNow . Subtract ( _lastReceivedMsg ) . TotalMilliseconds ) ;
96118 if ( diffMs > timeoutMs )
97119 {
98120 Logger . Debug ( L ( $ "Last message received more than { timeoutMs : F} ms ago. Hard restart..") ) ;
99121
100122 DeactivateLastChance ( ) ;
101123#pragma warning disable 4014
102- ReconnectSynchronized ( ReconnectionType . NoMessageReceived ) ;
124+ ReconnectSynchronized ( ReconnectionType . NoMessageReceived , false , null ) ;
103125#pragma warning restore 4014
104126 }
105127 }
0 commit comments