From 2e7bc099961867905814fb8e28ffd6e8c4b0c36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=E2=96=88=E2=96=88=E2=96=88=E2=96=88=E2=96=88?= Date: Tue, 15 Jul 2025 18:17:18 -0400 Subject: [PATCH] feat: propagating disconnect from twitch servers --- .../Twitch/ITwitchClientProxy.cs | 14 ++++++++++ .../Twitch/TwitchClientProxy.cs | 26 ++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/Nullinside.Api.Common/Twitch/ITwitchClientProxy.cs b/src/Nullinside.Api.Common/Twitch/ITwitchClientProxy.cs index 9d70354..6e3090e 100644 --- a/src/Nullinside.Api.Common/Twitch/ITwitchClientProxy.cs +++ b/src/Nullinside.Api.Common/Twitch/ITwitchClientProxy.cs @@ -73,4 +73,18 @@ public interface ITwitchClientProxy : IDisposable, IAsyncDisposable { /// The callback to remove. /// An asynchronous task. void RemoveRaidCallback(string channel, Action callback); + + /// + /// Adds a callback for being disconnected from the twitch chat server. + /// + /// The callback to invoke. + /// An asynchronous task. + void AddDisconnectedCallback(Action callback); + + /// + /// Removes a callback for being disconnected from the twitch chat server. + /// + /// The callback to remove. + /// An asynchronous task. + void RemoveDisconnectedCallback(Action callback); } \ No newline at end of file diff --git a/src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs b/src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs index de15c15..381a2e2 100644 --- a/src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs +++ b/src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs @@ -52,6 +52,11 @@ public class TwitchClientProxy : ITwitchClientProxy { /// The callback(s) to invoke when a channel receives a ban message. /// private readonly Dictionary?> _onUserBanReceived = new(); + + /// + /// The callback(s) to invoke when the twitch chat client is disconnected from twitch chat. + /// + private Action? _onDisconnected; /// /// A timer used to re-connect the Twitch chat client. @@ -73,6 +78,9 @@ public class TwitchClientProxy : ITwitchClientProxy { /// private WebSocketClient? _socket; + /// + /// The twitch OAuth token to use to connect. + /// private string? _twitchOAuthToken; /// @@ -227,6 +235,17 @@ public void RemoveBannedCallback(string channel, Action callba _onUserBanReceived.Remove(channel); } } + + /// + public void AddDisconnectedCallback(Action callback) { + _onDisconnected -= callback; + _onDisconnected += callback; + } + + /// + public void RemoveDisconnectedCallback(Action callback) { + _onDisconnected -= callback; + } /// public async Task AddRaidCallback(string channel, Action callback) { @@ -356,12 +375,7 @@ private Task Connect() { _client.OnRaidNotification += TwitchChatClient_OnRaidNotification; _client.OnDisconnected += (sender, args) => { LOG.Error("Twitch Client Disconnected"); - try { - _client.Reconnect(); - } - catch (Exception ex) { - LOG.Error("Twitch Client Reconnect Error", ex); - } + _onDisconnected?.Invoke(); }; _client.OnConnectionError += (sender, args) => { LOG.Error($"Twitch Client Connection Error: {args.Error.Message}");