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
|
|
|
|
{
|
|
|
|
private ILogger _logger { get; }
|
|
|
|
public int OperationCode { get; set; } = 0;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public HeartbeatHandler(ILogger logger)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
|
|
|
{
|
|
|
|
if (message is not HeartbeatMessage obj || obj == null)
|
|
|
|
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-06-16 20:19:31 -04:00
|
|
|
_logger.Verbose("Received heartbeat.");
|
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-16 20:19:31 -04:00
|
|
|
if (obj.Respond)
|
|
|
|
await sender.Send(0, new HeartbeatMessage()
|
|
|
|
{
|
|
|
|
DateTime = DateTime.UtcNow,
|
|
|
|
Respond = false
|
|
|
|
});
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|