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
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
await ConnectAsync($"wss://hermes-ws.goblincaves.com");
|
|
|
|
Connected = true;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (Connected)
|
|
|
|
{
|
|
|
|
_logger.Information("Reconnected.");
|
|
|
|
_reconnectTimer.Enabled = false;
|
|
|
|
_heartbeatTimer.Enabled = true;
|
|
|
|
LastHeartbeatReceived = DateTime.UtcNow;
|
|
|
|
|
|
|
|
if (_configuration.Hermes?.Token != null)
|
|
|
|
await Send(1, new HermesLoginMessage() { ApiKey = _configuration.Hermes.Token });
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|