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
|
|
|
|
{
|
|
|
|
private ILogger Logger { get; }
|
2024-06-16 20:19:31 -04:00
|
|
|
private User User { get; }
|
2024-03-12 14:05:27 -04:00
|
|
|
private IServiceProvider ServiceProvider { get; }
|
|
|
|
private string[] ErrorCodes { get; }
|
|
|
|
private int[] ReconnectDelay { get; }
|
|
|
|
|
|
|
|
public int OperationCode { get; set; } = 7;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
public EndOfStreamHandler(ILogger logger, User user, IServiceProvider serviceProvider)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
Logger = logger;
|
2024-06-16 20:19:31 -04:00
|
|
|
User = user;
|
2024-03-12 14:05:27 -04:00
|
|
|
ServiceProvider = serviceProvider;
|
|
|
|
|
|
|
|
ErrorCodes = [
|
|
|
|
"Server Error",
|
|
|
|
"Unknown Operation",
|
|
|
|
"Invalid Payload",
|
|
|
|
"Auth Failure",
|
|
|
|
"Already Identified",
|
|
|
|
"Rate Limited",
|
|
|
|
"Restart",
|
|
|
|
"Maintenance",
|
|
|
|
"Timeout",
|
|
|
|
"Already Subscribed",
|
|
|
|
"Not Subscribed",
|
|
|
|
"Insufficient Privilege",
|
|
|
|
"Inactivity?"
|
|
|
|
];
|
|
|
|
ReconnectDelay = [
|
|
|
|
1000,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
3000,
|
|
|
|
1000,
|
|
|
|
300000,
|
|
|
|
1000,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
1000,
|
|
|
|
1000
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
|
|
|
{
|
|
|
|
if (message is not EndOfStreamMessage obj || obj == null)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
var code = obj.Code - 4000;
|
|
|
|
if (code >= 0 && code < ErrorCodes.Length)
|
2024-06-16 20:19:31 -04:00
|
|
|
Logger.Warning($"Received end of stream message (reason: {ErrorCodes[code]}, code: {obj.Code}, message: {obj.Message}).");
|
2024-03-12 14:05:27 -04:00
|
|
|
else
|
2024-06-16 20:19:31 -04:00
|
|
|
Logger.Warning($"Received end of stream message (code: {obj.Code}, message: {obj.Message}).");
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
await sender.DisconnectAsync();
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (code >= 0 && code < ReconnectDelay.Length && ReconnectDelay[code] < 0)
|
|
|
|
{
|
|
|
|
Logger.Error($"7tv client will remain disconnected due to a bad client implementation.");
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (string.IsNullOrWhiteSpace(User.SevenEmoteSetId))
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
var context = ServiceProvider.GetRequiredService<ReconnectContext>();
|
|
|
|
await Task.Delay(ReconnectDelay[code]);
|
|
|
|
|
2024-06-16 20:19:31 -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-16 20:19:31 -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-06-16 20:19:31 -04:00
|
|
|
Logger.Information("Resumed connection to 7tv websocket.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Information("Resumed connection to 7tv websocket on a different session.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|