@@ -23,10 +23,15 @@ public class Proxy : IDisposable
2323 private ClientWebSocket _Socket ;
2424 private System . Timers . Timer _PingTimer ;
2525 private System . Timers . Timer _PongTimer ;
26+ private System . Timers . Timer _ReconnectTimer = new System . Timers . Timer ( ) ;
2627 private ConfigurationSettings _Configuration ;
2728 private ILogger _Logger ;
2829 private static bool _Reconnect ;
2930
31+ private static readonly TimeSpan [ ] _ReconnectTimeouts = new TimeSpan [ ] {
32+ TimeSpan . FromSeconds ( 10 ) , TimeSpan . FromSeconds ( 30 ) , TimeSpan . FromMinutes ( 1 ) , TimeSpan . FromMinutes ( 5 )
33+ } ;
34+
3035 public Proxy ( IOptions < ConfigurationSettings > settings , ILoggerFactory loggerFactory )
3136 {
3237
@@ -41,6 +46,7 @@ public async Task StartAsync(IEnumerable<TwitchTopic> topics, CancellationToken
4146 {
4247
4348 _Topics = topics ;
49+ _Reconnect = false ;
4450
4551 // Start a timer to manage the connection over the websocket
4652 _PingTimer = new System . Timers . Timer ( TimeSpan . FromSeconds ( 30 ) . TotalMilliseconds ) ;
@@ -83,12 +89,34 @@ public async Task StartAsync(IEnumerable<TwitchTopic> topics, CancellationToken
8389 }
8490
8591 if ( _Reconnect ) {
92+ if ( ! _ReconnectTimeouts . Any ( t => t . TotalMilliseconds == _ReconnectTimer . Interval ) ) {
93+ _ReconnectTimer . Interval = _ReconnectTimeouts [ 0 ] . TotalMilliseconds ;
94+ _Logger . LogError ( $ "Unable to connect to Twitch PubSub. Reconnecting in { _ReconnectTimeouts [ 0 ] . TotalSeconds } seconds") ;
95+ }
96+ else if ( _ReconnectTimeouts . Last ( ) . TotalMilliseconds == _ReconnectTimer . Interval ) {
97+ _Reconnect = false ;
98+ _Logger . LogError ( "Unable to connect to Twitch PubSub. Ceasing attempting to connect" ) ;
99+ } else {
100+
101+ for ( var i = 0 ; i < _ReconnectTimeouts . Length ; i ++ ) {
102+ if ( _ReconnectTimeouts [ i ] . TotalMilliseconds == _ReconnectTimer . Interval ) {
103+ _Logger . LogError ( $ "Unable to connect to Twitch PubSub. Reconnecting in { _ReconnectTimeouts [ i + 1 ] . TotalSeconds } seconds") ;
104+ _ReconnectTimer . Interval = _ReconnectTimeouts [ i + 1 ] . TotalMilliseconds ;
105+ break ;
106+ }
107+ }
108+
109+
110+ }
111+
112+ await Task . Delay ( ( int ) _ReconnectTimer . Interval ) ;
86113 break ;
114+
87115 }
88116
89117 }
90118
91- if ( _Reconnect ) _ = Task . Run ( ( ) => StartAsync ( topics , token ) ) ;
119+ // if (_Reconnect) _ = Task.Run(() => StartAsync(topics, token));
92120
93121 }
94122
@@ -126,7 +154,7 @@ private async Task StartListening(IEnumerable<TwitchTopic> topics)
126154 {
127155 data = new PubSubListen . PubSubListenData
128156 {
129- auth_token = _Configuration . OAuthToken ,
157+ auth_token = _Configuration . PubSubAuthToken ,
130158 topics = topics . Select ( t => t . TopicString ) . ToArray ( )
131159 }
132160 } ;
0 commit comments