Skip to content

Commit ad4e87a

Browse files
Merge pull request #109 from nullinside-development-group/feat/disconnect
feat: propagating disconnect from twitch servers
2 parents 513c53b + 2e7bc09 commit ad4e87a

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/Nullinside.Api.Common/Twitch/ITwitchClientProxy.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,18 @@ public interface ITwitchClientProxy : IDisposable, IAsyncDisposable {
7373
/// <param name="callback">The callback to remove.</param>
7474
/// <returns>An asynchronous task.</returns>
7575
void RemoveRaidCallback(string channel, Action<OnRaidNotificationArgs> callback);
76+
77+
/// <summary>
78+
/// Adds a callback for being disconnected from the twitch chat server.
79+
/// </summary>
80+
/// <param name="callback">The callback to invoke.</param>
81+
/// <returns>An asynchronous task.</returns>
82+
void AddDisconnectedCallback(Action callback);
83+
84+
/// <summary>
85+
/// Removes a callback for being disconnected from the twitch chat server.
86+
/// </summary>
87+
/// <param name="callback">The callback to remove.</param>
88+
/// <returns>An asynchronous task.</returns>
89+
void RemoveDisconnectedCallback(Action callback);
7690
}

src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class TwitchClientProxy : ITwitchClientProxy {
5252
/// The callback(s) to invoke when a channel receives a ban message.
5353
/// </summary>
5454
private readonly Dictionary<string, Action<OnUserBannedArgs>?> _onUserBanReceived = new();
55+
56+
/// <summary>
57+
/// The callback(s) to invoke when the twitch chat client is disconnected from twitch chat.
58+
/// </summary>
59+
private Action? _onDisconnected;
5560

5661
/// <summary>
5762
/// A timer used to re-connect the Twitch chat client.
@@ -73,6 +78,9 @@ public class TwitchClientProxy : ITwitchClientProxy {
7378
/// </summary>
7479
private WebSocketClient? _socket;
7580

81+
/// <summary>
82+
/// The twitch OAuth token to use to connect.
83+
/// </summary>
7684
private string? _twitchOAuthToken;
7785

7886
/// <summary>
@@ -227,6 +235,17 @@ public void RemoveBannedCallback(string channel, Action<OnUserBannedArgs> callba
227235
_onUserBanReceived.Remove(channel);
228236
}
229237
}
238+
239+
/// <inheritdoc />
240+
public void AddDisconnectedCallback(Action callback) {
241+
_onDisconnected -= callback;
242+
_onDisconnected += callback;
243+
}
244+
245+
/// <inheritdoc />
246+
public void RemoveDisconnectedCallback(Action callback) {
247+
_onDisconnected -= callback;
248+
}
230249

231250
/// <inheritdoc />
232251
public async Task AddRaidCallback(string channel, Action<OnRaidNotificationArgs> callback) {
@@ -356,12 +375,7 @@ private Task<bool> Connect() {
356375
_client.OnRaidNotification += TwitchChatClient_OnRaidNotification;
357376
_client.OnDisconnected += (sender, args) => {
358377
LOG.Error("Twitch Client Disconnected");
359-
try {
360-
_client.Reconnect();
361-
}
362-
catch (Exception ex) {
363-
LOG.Error("Twitch Client Reconnect Error", ex);
364-
}
378+
_onDisconnected?.Invoke();
365379
};
366380
_client.OnConnectionError += (sender, args) => {
367381
LOG.Error($"Twitch Client Connection Error: {args.Error.Message}");

0 commit comments

Comments
 (0)