2024-06-24 18:21:59 -04:00
|
|
|
using HermesSocketLibrary.Socket.Data;
|
|
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
|
|
|
|
namespace HermesSocketServer.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class HeartbeatHandler : ISocketHandler
|
|
|
|
{
|
2024-08-10 15:36:32 -04:00
|
|
|
public int OperationCode { get; } = 0;
|
2024-06-24 18:21:59 -04:00
|
|
|
|
|
|
|
private ILogger _logger;
|
|
|
|
|
|
|
|
public HeartbeatHandler(ILogger logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<T>(WebSocketUser sender, T message, HermesSocketManager sockets)
|
|
|
|
{
|
|
|
|
if (message is not HeartbeatMessage data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sender.LastHeartbeatReceived = DateTime.UtcNow;
|
|
|
|
_logger.Verbose($"Received heartbeat from socket [ip: {sender.IPAddress}].");
|
|
|
|
|
|
|
|
if (data.Respond)
|
2024-08-10 15:36:32 -04:00
|
|
|
{
|
|
|
|
sender.LastHearbeatSent = DateTime.UtcNow;
|
2024-06-24 18:21:59 -04:00
|
|
|
await sender.Send(0, new HeartbeatMessage()
|
|
|
|
{
|
|
|
|
DateTime = DateTime.UtcNow,
|
|
|
|
Respond = false
|
|
|
|
});
|
2024-08-10 15:36:32 -04:00
|
|
|
}
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|