2024-03-12 14:05:27 -04:00
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-12 14:05:27 -04:00
|
|
|
using TwitchChatTTS.Seven.Socket.Context;
|
|
|
|
using TwitchChatTTS.Seven.Socket.Data;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Seven.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class EndOfStreamHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly User _user;
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
private readonly string[] _errorCodes;
|
|
|
|
private readonly int[] _reconnectDelay;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public int OperationCode { get; } = 7;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-12 13:36:09 -04:00
|
|
|
public EndOfStreamHandler(User user, IServiceProvider serviceProvider, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger = logger;
|
|
|
|
_user = user;
|
|
|
|
_serviceProvider = serviceProvider;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
_errorCodes = [
|
2024-03-12 14:05:27 -04:00
|
|
|
"Server Error",
|
|
|
|
"Unknown Operation",
|
|
|
|
"Invalid Payload",
|
|
|
|
"Auth Failure",
|
|
|
|
"Already Identified",
|
|
|
|
"Rate Limited",
|
|
|
|
"Restart",
|
|
|
|
"Maintenance",
|
|
|
|
"Timeout",
|
|
|
|
"Already Subscribed",
|
|
|
|
"Not Subscribed",
|
|
|
|
"Insufficient Privilege",
|
|
|
|
"Inactivity?"
|
|
|
|
];
|
2024-06-24 18:11:36 -04:00
|
|
|
_reconnectDelay = [
|
2024-03-12 14:05:27 -04:00
|
|
|
1000,
|
2024-06-24 18:11:36 -04:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2024-03-12 14:05:27 -04:00
|
|
|
3000,
|
|
|
|
1000,
|
|
|
|
300000,
|
|
|
|
1000,
|
2024-06-24 18:11:36 -04:00
|
|
|
0,
|
|
|
|
0,
|
2024-03-12 14:05:27 -04:00
|
|
|
1000,
|
|
|
|
1000
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
2024-03-12 14:05:27 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (data is not EndOfStreamMessage message || message == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
var code = message.Code - 4000;
|
|
|
|
if (code >= 0 && code < _errorCodes.Length)
|
|
|
|
_logger.Warning($"Received end of stream message (reason: {_errorCodes[code]}, code: {message.Code}, message: {message.Message}).");
|
2024-03-12 14:05:27 -04:00
|
|
|
else
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Warning($"Received end of stream message (code: {message.Code}, message: {message.Message}).");
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
await sender.DisconnectAsync();
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
if (code >= 0 && code < _reconnectDelay.Length && _reconnectDelay[code] < 0)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Error($"7tv client will remain disconnected due to a bad client implementation.");
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
if (string.IsNullOrWhiteSpace(_user.SevenEmoteSetId))
|
2024-07-12 13:36:09 -04:00
|
|
|
{
|
|
|
|
_logger.Warning("Connected to 7tv websocket previously, but no emote set id was set.");
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-07-12 13:36:09 -04:00
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
var context = _serviceProvider.GetRequiredService<ReconnectContext>();
|
|
|
|
if (_reconnectDelay[code] > 0)
|
|
|
|
await Task.Delay(_reconnectDelay[code]);
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
var base_url = $"@emote_set.*<object_id={_user.SevenEmoteSetId}>";
|
2024-03-15 08:27:35 -04:00
|
|
|
string url = $"{SevenApiClient.WEBSOCKET_URL}{base_url}";
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Debug($"7tv websocket reconnecting to {url}.");
|
2024-03-15 08:27:35 -04:00
|
|
|
|
|
|
|
await sender.ConnectAsync(url);
|
2024-06-16 20:19:31 -04:00
|
|
|
if (context.SessionId != null)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
await sender.Send(34, new ResumeMessage() { SessionId = context.SessionId });
|
2024-07-12 13:36:09 -04:00
|
|
|
_logger.Debug("Resumed connection to 7tv websocket.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-07-12 13:36:09 -04:00
|
|
|
_logger.Debug("Resumed connection to 7tv websocket on a different session.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|