@@ -131,6 +131,11 @@ public class CrowControlSettings : EverestModuleSettings
131131 private Thread webSocketThread ;
132132 private Random rand = new Random ( ) ;
133133
134+ // better handle socket disconnections
135+ public bool IsExiting = false ;
136+ private CancellationTokenSource cancelToken ;
137+ private bool clickedDisconnect = false ;
138+
134139 [ YamlIgnore ] public Dictionary < MessageType , List < string > > currentVoteCounts = new Dictionary < MessageType , List < string > > ( ) ;
135140
136141 public CrowControlSettings ( )
@@ -150,11 +155,6 @@ public void EffectTimeChange()
150155 CrowControlModule . GetTimerHelper ( ) . ChangeTimerIntervals ( ) ;
151156 }
152157
153- public void ClearAllVotes ( )
154- {
155-
156- }
157-
158158 public void CreateChannelNameEntry ( TextMenu textMenu , bool inGame )
159159 {
160160 if ( ! inGame )
@@ -176,6 +176,9 @@ public void CreateConnectEntry(TextMenu textMenu, bool inGame)
176176 {
177177 if ( webSocketThread == null && Enabled )
178178 {
179+ IsReconnecting = false ;
180+ clickedDisconnect = false ;
181+ cancelToken = new CancellationTokenSource ( ) ;
179182 webSocketThread = new Thread ( StartWebSocket ) ;
180183 webSocketThread . Start ( ) ;
181184 }
@@ -188,6 +191,8 @@ public void CreateDisconnectEntry(TextMenu textMenu, bool inGame)
188191 {
189192 if ( webSocketThread != null && webSocketThread . IsAlive )
190193 {
194+ IsReconnecting = false ;
195+ clickedDisconnect = true ;
191196 StopThread ( ) ;
192197 }
193198 } ) ) ;
@@ -203,7 +208,7 @@ public void StopThread()
203208 {
204209 if ( webSocketThread != null && webSocketThread . IsAlive )
205210 {
206- Enabled = false ;
211+ cancelToken . Cancel ( ) ;
207212 webSocketThread . Join ( ) ;
208213 webSocketThread = null ;
209214 }
@@ -233,17 +238,13 @@ public void StartWebSocket()
233238 Console . WriteLine ( "JOINING" ) ;
234239
235240 IsReconnecting = false ;
236-
237- while ( Enabled )
238- {
239-
240- }
241+ cancelToken . Token . WaitHandle . WaitOne ( ) ;
241242 }
242243 }
243244
244245 private void ReconnectIfDisconnected ( )
245246 {
246- if ( ReconnectOnDisconnect )
247+ if ( ReconnectOnDisconnect && ! IsExiting && ! clickedDisconnect )
247248 {
248249 if ( Connected == false )
249250 {
@@ -254,7 +255,7 @@ private void ReconnectIfDisconnected()
254255
255256 if ( webSocketThread == null )
256257 {
257- Enabled = true ;
258+ cancelToken = new CancellationTokenSource ( ) ;
258259 webSocketThread = new Thread ( StartWebSocket ) ;
259260 webSocketThread . Start ( ) ;
260261 }
@@ -279,7 +280,10 @@ private void Ws_OnClose(object sender, CloseEventArgs e)
279280
280281 CrowControlModule . OnDisconnect ( ) ;
281282 Connected = false ;
282- ReconnectIfDisconnected ( ) ;
283+ if ( ! clickedDisconnect )
284+ ReconnectIfDisconnected ( ) ;
285+ // Reset this flag
286+ clickedDisconnect = false ;
283287 }
284288
285289 private void Ws_OnOpen ( object sender , EventArgs e )
@@ -303,10 +307,8 @@ private void Ws_OnMessage(object sender, MessageEventArgs e)
303307 if ( chatMsg . Bits >= MinimumBitsToSkip && MinimumBitsToSkip > 0 )
304308 {
305309 CrowControlModule . OnMessageWithBits ( chatMsg ) ;
306- return ;
307310 }
308-
309- if ( ( chatMsg . IsCustomReward && RequireChannelPoints ) || ! RequireChannelPoints )
311+ else if ( ( chatMsg . IsCustomReward && RequireChannelPoints ) || ! RequireChannelPoints )
310312 {
311313 CrowControlModule . OnCustomRewardMessage ( chatMsg ) ;
312314 }
0 commit comments