Prevent Twitch websocket client to subscribe again if Twitch moved/reconnected us

This commit is contained in:
Tom 2024-08-12 18:06:10 +00:00
parent 693a448972
commit f503f7c6f4
3 changed files with 44 additions and 64 deletions

View File

@ -43,6 +43,7 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
_logger.Debug($"Reconnection received [receiver: {sender.UID}][main: {identified.UID}][backup: {backup.UID}]"); _logger.Debug($"Reconnection received [receiver: {sender.UID}][main: {identified.UID}][backup: {backup.UID}]");
backup.URL = message.Session.ReconnectUrl; backup.URL = message.Session.ReconnectUrl;
backup.TwitchReconnected = true;
await backup.Connect(); await backup.Connect();
} }
} }

View File

@ -46,11 +46,14 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
_logger.Information($"Twitch connection has {timeLeft} before it is revoked."); _logger.Information($"Twitch connection has {timeLeft} before it is revoked.");
else if (span.Minutes >= 0) else if (span.Minutes >= 0)
_logger.Warning($"Twitch connection has {timeLeft} before it is revoked. Refreshing the token is soon required."); _logger.Warning($"Twitch connection has {timeLeft} before it is revoked. Refreshing the token is soon required.");
else { else
{
_logger.Error("Twitch connection has its permissions revoked. Refresh the token. Twith client will not be connecting."); _logger.Error("Twitch connection has its permissions revoked. Refresh the token. Twith client will not be connecting.");
return; return;
} }
if (!sender.TwitchReconnected)
{
string broadcasterId = _user.TwitchUserId.ToString(); string broadcasterId = _user.TwitchUserId.ToString();
string[] subscriptionsv1 = [ string[] subscriptionsv1 = [
"channel.chat.message", "channel.chat.message",
@ -94,39 +97,14 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "2"); await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "2");
await Subscribe(sender, "channel.raid", broadcasterId, async () => await _api.CreateChannelRaidEventSubscription("1", message.Session.Id, to: broadcasterId)); await Subscribe(sender, "channel.raid", broadcasterId, async () => await _api.CreateChannelRaidEventSubscription("1", message.Session.Id, to: broadcasterId));
}
sender.Identify(message.Session.Id); sender.Identify(message.Session.Id);
} }
private async Task Subscribe(TwitchWebsocketClient sender, string subscriptionName, string sessionId, string broadcasterId, string version) private async Task Subscribe(TwitchWebsocketClient sender, string subscriptionName, string sessionId, string broadcasterId, string version)
{ {
try await Subscribe(sender, subscriptionName, broadcasterId, async () => await _api.CreateEventSubscription(subscriptionName, version, sessionId, broadcasterId));
{
var response = await _api.CreateEventSubscription(subscriptionName, version, sessionId, broadcasterId);
if (response == null)
{
return;
}
if (response.Data == null)
{
_logger.Error($"Failed to create an event subscription [subscription type: {subscriptionName}][reason: data is null]");
return;
}
if (!response.Data.Any())
{
_logger.Error($"Failed to create an event subscription [subscription type: {subscriptionName}][reason: data is empty]");
return;
}
foreach (var d in response.Data)
sender.AddSubscription(broadcasterId, d.Type, d.Id);
_logger.Information($"Sucessfully added subscription to Twitch websockets [subscription type: {subscriptionName}]");
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to create an event subscription [subscription type: {subscriptionName}][reason: exception]");
}
} }
private async Task Subscribe(TwitchWebsocketClient sender, string subscriptionName, string broadcasterId, Func<Task<EventResponse<NotificationInfo>?>> subscribe) private async Task Subscribe(TwitchWebsocketClient sender, string subscriptionName, string broadcasterId, Func<Task<EventResponse<NotificationInfo>?>> subscribe)

View File

@ -29,6 +29,7 @@ namespace TwitchChatTTS.Twitch.Socket
public bool Identified { get; private set; } public bool Identified { get; private set; }
public string SessionId { get; private set; } public string SessionId { get; private set; }
public bool ReceivedReconnecting { get; set; } public bool ReceivedReconnecting { get; set; }
public bool TwitchReconnected { get; set; }
public TwitchWebsocketClient( public TwitchWebsocketClient(