@@ -20,11 +20,17 @@ public class Proxy : IDisposable {
20
20
private ClientWebSocket _Socket ;
21
21
private System . Timers . Timer _PingTimer ;
22
22
private System . Timers . Timer _PongTimer ;
23
- private readonly ConfigurationSettings _Configuration ;
24
- private readonly ILogger _Logger ;
23
+ private System . Timers . Timer _ReconnectTimer = new System . Timers . Timer ( ) ;
24
+ private ConfigurationSettings _Configuration ;
25
+ private ILogger _Logger ;
25
26
private static bool _Reconnect ;
26
27
27
- public Proxy ( IOptions < ConfigurationSettings > settings , ILoggerFactory loggerFactory ) {
28
+ private static readonly TimeSpan [ ] _ReconnectTimeouts = new TimeSpan [ ] {
29
+ TimeSpan . FromSeconds ( 10 ) , TimeSpan . FromSeconds ( 30 ) , TimeSpan . FromMinutes ( 1 ) , TimeSpan . FromMinutes ( 5 )
30
+ } ;
31
+
32
+ public Proxy ( IOptions < ConfigurationSettings > settings , ILoggerFactory loggerFactory )
33
+ {
28
34
29
35
InitializeMethodStrategies ( ) ;
30
36
@@ -36,6 +42,7 @@ public Proxy(IOptions<ConfigurationSettings> settings, ILoggerFactory loggerFact
36
42
public async Task StartAsync ( IEnumerable < TwitchTopic > topics , CancellationToken token ) {
37
43
38
44
_Topics = topics ;
45
+ _Reconnect = false ;
39
46
40
47
// Start a timer to manage the connection over the websocket
41
48
_PingTimer = new System . Timers . Timer ( TimeSpan . FromSeconds ( 30 ) . TotalMilliseconds ) ;
@@ -77,14 +84,35 @@ public async Task StartAsync(IEnumerable<TwitchTopic> topics, CancellationToken
77
84
}
78
85
79
86
if ( _Reconnect ) {
87
+ if ( ! _ReconnectTimeouts . Any ( t => t . TotalMilliseconds == _ReconnectTimer . Interval ) ) {
88
+ _ReconnectTimer . Interval = _ReconnectTimeouts [ 0 ] . TotalMilliseconds ;
89
+ _Logger . LogError ( $ "Unable to connect to Twitch PubSub. Reconnecting in { _ReconnectTimeouts [ 0 ] . TotalSeconds } seconds") ;
90
+ }
91
+ else if ( _ReconnectTimeouts . Last ( ) . TotalMilliseconds == _ReconnectTimer . Interval ) {
92
+ _Reconnect = false ;
93
+ _Logger . LogError ( "Unable to connect to Twitch PubSub. Ceasing attempting to connect" ) ;
94
+ } else {
95
+
96
+ for ( var i = 0 ; i < _ReconnectTimeouts . Length ; i ++ ) {
97
+ if ( _ReconnectTimeouts [ i ] . TotalMilliseconds == _ReconnectTimer . Interval ) {
98
+ _Logger . LogError ( $ "Unable to connect to Twitch PubSub. Reconnecting in { _ReconnectTimeouts [ i + 1 ] . TotalSeconds } seconds") ;
99
+ _ReconnectTimer . Interval = _ReconnectTimeouts [ i + 1 ] . TotalMilliseconds ;
100
+ break ;
101
+ }
102
+ }
103
+
104
+
105
+ }
106
+
107
+ await Task . Delay ( ( int ) _ReconnectTimer . Interval ) ;
80
108
break ;
109
+
81
110
}
82
111
83
112
}
84
113
85
- if ( _Reconnect ) {
86
- _ = Task . Run ( ( ) => StartAsync ( topics , token ) ) ;
87
- }
114
+ // if (_Reconnect) _ = Task.Run(() => StartAsync(topics, token));
115
+
88
116
}
89
117
90
118
0 commit comments