2024-03-15 08:27:35 -04:00
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
|
|
|
using HermesSocketLibrary.Socket.Data;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
|
|
|
namespace TwitchChatTTS.Hermes.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class HeartbeatHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
public int OperationCode { get; } = 0;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public HeartbeatHandler(ILogger logger)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
2024-03-15 08:27:35 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (data is not HeartbeatMessage message || message == null)
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
if (sender is not HermesSocketClient client)
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
|
|
|
|
2024-07-12 13:36:09 -04:00
|
|
|
_logger.Verbose("Received heartbeat from server.");
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
client.LastHeartbeatReceived = DateTime.UtcNow;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
if (message.Respond)
|
2024-06-16 20:19:31 -04:00
|
|
|
await sender.Send(0, new HeartbeatMessage()
|
|
|
|
{
|
|
|
|
DateTime = DateTime.UtcNow,
|
|
|
|
Respond = false
|
|
|
|
});
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|