2024-08-04 19:46:10 -04:00
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using Serilog;
|
|
|
|
using TwitchChatTTS.Twitch.Socket.Messages;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Twitch.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class SessionReconnectHandler : ITwitchSocketHandler
|
|
|
|
{
|
|
|
|
public string Name => "session_reconnect";
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
private readonly ITwitchConnectionManager _manager;
|
2024-08-04 19:46:10 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public SessionReconnectHandler(ITwitchConnectionManager manager, ILogger logger)
|
2024-08-04 19:46:10 -04:00
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
_manager = manager;
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public async Task Execute(TwitchWebsocketClient sender, object data)
|
2024-08-04 19:46:10 -04:00
|
|
|
{
|
|
|
|
if (sender == null)
|
|
|
|
return;
|
|
|
|
if (data is not SessionWelcomeMessage message)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(message.Session.Id))
|
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
_logger.Warning($"No session id provided by Twitch [status: {message.Session.Status}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.Session.ReconnectUrl == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"No reconnection info provided by Twitch [status: {message.Session.Status}]");
|
2024-08-04 19:46:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
sender.ReceivedReconnecting = true;
|
|
|
|
|
|
|
|
var backup = _manager.GetBackupClient();
|
|
|
|
var identified = _manager.GetWorkingClient();
|
|
|
|
if (identified != null && backup != identified)
|
|
|
|
{
|
|
|
|
await identified.DisconnectAsync(new SocketDisconnectionEventArgs("Closed", "Reconnection from another client."));
|
|
|
|
}
|
|
|
|
|
|
|
|
backup.URL = message.Session.ReconnectUrl;
|
|
|
|
await backup.Connect();
|
2024-08-04 19:46:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|