Added a Websocket disconnection event to Shuffle that rebuilds the Shuffle connection if it's forcibly disconnected. This seems to have resolved issues with the bot disconnecting from Shuffle and never reconnecting.

This commit is contained in:
barelyprofessional
2024-06-23 13:26:30 +08:00
parent ff2a4c0e2e
commit 4aef087f3b
2 changed files with 24 additions and 7 deletions

View File

@@ -27,7 +27,7 @@ public class KickBot
private bool _initialStartCooldown = true; private bool _initialStartCooldown = true;
private readonly CancellationToken _cancellationToken = new(); private readonly CancellationToken _cancellationToken = new();
private readonly Twitch _twitch; private readonly Twitch _twitch;
private readonly Shuffle _shuffle; private Shuffle _shuffle;
private bool _isBmjLive = false; private bool _isBmjLive = false;
private bool _isBmjLiveSynced = false; private bool _isBmjLiveSynced = false;
@@ -94,16 +94,30 @@ public class KickBot
_logger.Debug("Ignoring Twitch client as TwitchChannels is not defined"); _logger.Debug("Ignoring Twitch client as TwitchChannels is not defined");
} }
_shuffle = new Shuffle(_config.Proxy, _cancellationToken); BuildShuffle();
_shuffle.OnLatestBetUpdated += ShuffleOnOnLatestBetUpdated;
_shuffle.StartWsClient().Wait(_cancellationToken);
_logger.Debug("Blocking the main thread"); _logger.Debug("Blocking the main thread");
var exitEvent = new ManualResetEvent(false); var exitEvent = new ManualResetEvent(false);
exitEvent.WaitOne(); exitEvent.WaitOne();
} }
private void ShuffleOnOnLatestBetUpdated(object sender, ShuffleLatestBetModel bet) private void BuildShuffle()
{
_shuffle = new Shuffle(_config.Proxy, _cancellationToken);
_shuffle.OnLatestBetUpdated += ShuffleOnLatestBetUpdated;
_shuffle.OnWsDisconnection += ShuffleOnWsDisconnection;
_shuffle.StartWsClient().Wait(_cancellationToken);
}
private void ShuffleOnWsDisconnection(object sender, DisconnectionInfo e)
{
if (e.Type == DisconnectionType.ByServer)
{
BuildShuffle();
}
}
private void ShuffleOnLatestBetUpdated(object sender, ShuffleLatestBetModel bet)
{ {
_logger.Debug("Shuffle bet has arrived"); _logger.Debug("Shuffle bet has arrived");
if (bet.Username != "TheBossmanJack") if (bet.Username != "TheBossmanJack")

View File

@@ -16,7 +16,9 @@ public class Shuffle
private int _reconnectTimeout = 60; private int _reconnectTimeout = 60;
private string? _proxy; private string? _proxy;
public delegate void OnLatestBetUpdatedEventHandler(object sender, ShuffleLatestBetModel bet); public delegate void OnLatestBetUpdatedEventHandler(object sender, ShuffleLatestBetModel bet);
public delegate void OnWsDisconnectionEventHandler(object sender, DisconnectionInfo e);
public event OnLatestBetUpdatedEventHandler OnLatestBetUpdated; public event OnLatestBetUpdatedEventHandler OnLatestBetUpdated;
public event OnWsDisconnectionEventHandler OnWsDisconnection;
private CancellationToken _cancellationToken = CancellationToken.None; private CancellationToken _cancellationToken = CancellationToken.None;
public Shuffle(string? proxy = null, CancellationToken? cancellationToken = null) public Shuffle(string? proxy = null, CancellationToken? cancellationToken = null)
@@ -71,7 +73,7 @@ public class Shuffle
private void SendPing() private void SendPing()
{ {
_logger.Debug("Sending ping to Shuffle"); _logger.Debug("Sending ping to Shuffle");
_wsClient.Send("{\"type\":\"ping\"}"); _wsClient.SendInstant("{\"type\":\"ping\"}").Wait(_cancellationToken);
} }
private async Task PeriodicPing() private async Task PeriodicPing()
@@ -86,7 +88,7 @@ public class Shuffle
} }
if (!IsConnected()) if (!IsConnected())
{ {
_logger.Debug("Not connected not going to try send a ping actually"); _logger.Info("Not connected not going to try send a ping actually");
continue; continue;
} }
SendPing(); SendPing();
@@ -97,6 +99,7 @@ public class Shuffle
{ {
_logger.Error($"Client disconnected from the chat (or never successfully connected). Type is {disconnectionInfo.Type}"); _logger.Error($"Client disconnected from the chat (or never successfully connected). Type is {disconnectionInfo.Type}");
_logger.Error(disconnectionInfo.Exception); _logger.Error(disconnectionInfo.Exception);
OnWsDisconnection?.Invoke(this, disconnectionInfo);
} }
private void WsReconnection(ReconnectionInfo reconnectionInfo) private void WsReconnection(ReconnectionInfo reconnectionInfo)