2024-03-15 08:27:35 -04:00
|
|
|
using System.Text.Json;
|
2024-06-16 20:19:31 -04:00
|
|
|
using System.Timers;
|
2024-03-15 08:27:35 -04:00
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
2024-06-16 20:19:31 -04:00
|
|
|
using HermesSocketLibrary.Socket.Data;
|
2024-03-15 08:27:35 -04:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
|
|
|
namespace TwitchChatTTS.Hermes.Socket
|
|
|
|
{
|
2024-06-16 20:19:31 -04:00
|
|
|
public class HermesSocketClient : WebSocketClient
|
|
|
|
{
|
|
|
|
private Configuration _configuration;
|
|
|
|
public DateTime LastHeartbeatReceived { get; set; }
|
|
|
|
public DateTime LastHeartbeatSent { get; set; }
|
|
|
|
public string? UserId { get; set; }
|
|
|
|
private System.Timers.Timer _heartbeatTimer;
|
|
|
|
private System.Timers.Timer _reconnectTimer;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-07-12 13:36:09 -04:00
|
|
|
public const string BASE_URL = "ws.tomtospeech.com";
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
public HermesSocketClient(
|
2024-06-16 20:19:31 -04:00
|
|
|
Configuration configuration,
|
2024-03-15 08:27:35 -04:00
|
|
|
[FromKeyedServices("hermes")] HandlerManager<WebSocketClient, IWebSocketHandler> handlerManager,
|
2024-06-16 20:19:31 -04:00
|
|
|
[FromKeyedServices("hermes")] HandlerTypeManager<WebSocketClient, IWebSocketHandler> typeManager,
|
|
|
|
ILogger logger
|
|
|
|
) : base(logger, handlerManager, typeManager, new JsonSerializerOptions()
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
2024-06-16 20:19:31 -04:00
|
|
|
})
|
|
|
|
{
|
|
|
|
_configuration = configuration;
|
|
|
|
|
|
|
|
_heartbeatTimer = new System.Timers.Timer(TimeSpan.FromSeconds(15));
|
|
|
|
_heartbeatTimer.Elapsed += async (sender, e) => await HandleHeartbeat(e);
|
|
|
|
|
|
|
|
_reconnectTimer = new System.Timers.Timer(TimeSpan.FromSeconds(15));
|
|
|
|
_reconnectTimer.Elapsed += async (sender, e) => await Reconnect(e);
|
|
|
|
|
|
|
|
LastHeartbeatReceived = LastHeartbeatSent = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task OnConnection()
|
|
|
|
{
|
|
|
|
_heartbeatTimer.Enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task HandleHeartbeat(ElapsedEventArgs e)
|
|
|
|
{
|
|
|
|
var signalTime = e.SignalTime.ToUniversalTime();
|
|
|
|
|
|
|
|
if (signalTime - LastHeartbeatReceived > TimeSpan.FromSeconds(60))
|
|
|
|
{
|
|
|
|
if (LastHeartbeatReceived > LastHeartbeatSent)
|
|
|
|
{
|
|
|
|
_logger.Verbose("Sending heartbeat...");
|
|
|
|
LastHeartbeatSent = DateTime.UtcNow;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await Send(0, new HeartbeatMessage() { DateTime = LastHeartbeatSent });
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (signalTime - LastHeartbeatReceived > TimeSpan.FromSeconds(120))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await DisconnectAsync();
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
UserId = null;
|
|
|
|
_heartbeatTimer.Enabled = false;
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Warning("Logged off due to disconnection. Attempting to reconnect...");
|
2024-06-16 20:19:31 -04:00
|
|
|
_reconnectTimer.Enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task Reconnect(ElapsedEventArgs e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-07-12 13:36:09 -04:00
|
|
|
await ConnectAsync($"wss://{HermesSocketClient.BASE_URL}");
|
2024-06-16 20:19:31 -04:00
|
|
|
Connected = true;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (Connected)
|
|
|
|
{
|
|
|
|
_logger.Information("Reconnected.");
|
|
|
|
_reconnectTimer.Enabled = false;
|
|
|
|
_heartbeatTimer.Enabled = true;
|
|
|
|
LastHeartbeatReceived = DateTime.UtcNow;
|
|
|
|
|
|
|
|
if (_configuration.Hermes?.Token != null)
|
2024-07-06 23:42:33 -04:00
|
|
|
await Send(1, new HermesLoginMessage()
|
|
|
|
{
|
|
|
|
ApiKey = _configuration.Hermes.Token,
|
|
|
|
MajorVersion = TTS.MAJOR_VERSION,
|
|
|
|
MinorVersion = TTS.MINOR_VERSION,
|
|
|
|
});
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|