Prevent Twitch websocket client to subscribe again if Twitch moved/reconnected us
This commit is contained in:
parent
693a448972
commit
f503f7c6f4
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,87 +46,65 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
string broadcasterId = _user.TwitchUserId.ToString();
|
if (!sender.TwitchReconnected)
|
||||||
string[] subscriptionsv1 = [
|
|
||||||
"channel.chat.message",
|
|
||||||
"channel.chat.message_delete",
|
|
||||||
"channel.chat.clear",
|
|
||||||
"channel.chat.clear_user_messages",
|
|
||||||
"channel.subscribe",
|
|
||||||
"channel.subscription.gift",
|
|
||||||
"channel.subscription.message",
|
|
||||||
"channel.ad_break.begin",
|
|
||||||
"channel.ban",
|
|
||||||
"channel.channel_points_custom_reward_redemption.add"
|
|
||||||
];
|
|
||||||
string[] subscriptionsv2 = [
|
|
||||||
"channel.follow",
|
|
||||||
];
|
|
||||||
|
|
||||||
string? pagination = null;
|
|
||||||
int size = 0;
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
var subscriptionsData = await _api.GetSubscriptions(status: "enabled", broadcasterId: broadcasterId, after: pagination);
|
string broadcasterId = _user.TwitchUserId.ToString();
|
||||||
var subscriptionNames = subscriptionsData?.Data == null ? [] : subscriptionsData.Data.Select(s => s.Type).ToArray();
|
string[] subscriptionsv1 = [
|
||||||
|
"channel.chat.message",
|
||||||
|
"channel.chat.message_delete",
|
||||||
|
"channel.chat.clear",
|
||||||
|
"channel.chat.clear_user_messages",
|
||||||
|
"channel.subscribe",
|
||||||
|
"channel.subscription.gift",
|
||||||
|
"channel.subscription.message",
|
||||||
|
"channel.ad_break.begin",
|
||||||
|
"channel.ban",
|
||||||
|
"channel.channel_points_custom_reward_redemption.add"
|
||||||
|
];
|
||||||
|
string[] subscriptionsv2 = [
|
||||||
|
"channel.follow",
|
||||||
|
];
|
||||||
|
|
||||||
if (subscriptionNames.Length == 0)
|
string? pagination = null;
|
||||||
break;
|
int size = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
var subscriptionsData = await _api.GetSubscriptions(status: "enabled", broadcasterId: broadcasterId, after: pagination);
|
||||||
|
var subscriptionNames = subscriptionsData?.Data == null ? [] : subscriptionsData.Data.Select(s => s.Type).ToArray();
|
||||||
|
|
||||||
foreach (var d in subscriptionsData!.Data!)
|
if (subscriptionNames.Length == 0)
|
||||||
sender.AddSubscription(broadcasterId, d.Type, d.Id);
|
break;
|
||||||
|
|
||||||
subscriptionsv1 = subscriptionsv1.Except(subscriptionNames).ToArray();
|
foreach (var d in subscriptionsData!.Data!)
|
||||||
subscriptionsv2 = subscriptionsv2.Except(subscriptionNames).ToArray();
|
sender.AddSubscription(broadcasterId, d.Type, d.Id);
|
||||||
|
|
||||||
pagination = subscriptionsData?.Pagination?.Cursor;
|
subscriptionsv1 = subscriptionsv1.Except(subscriptionNames).ToArray();
|
||||||
size = subscriptionNames.Length;
|
subscriptionsv2 = subscriptionsv2.Except(subscriptionNames).ToArray();
|
||||||
} while (size >= 100 && pagination != null && subscriptionsv1.Length + subscriptionsv2.Length > 0);
|
|
||||||
|
|
||||||
foreach (var subscription in subscriptionsv1)
|
pagination = subscriptionsData?.Pagination?.Cursor;
|
||||||
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "1");
|
size = subscriptionNames.Length;
|
||||||
foreach (var subscription in subscriptionsv2)
|
} while (size >= 100 && pagination != null && subscriptionsv1.Length + subscriptionsv2.Length > 0);
|
||||||
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));
|
foreach (var subscription in subscriptionsv1)
|
||||||
|
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "1");
|
||||||
|
foreach (var subscription in subscriptionsv2)
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
@ -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(
|
||||||
|
Loading…
Reference in New Issue
Block a user