hermes-server/Socket/Handlers/HeartbeatHandler.cs

36 lines
1013 B
C#
Raw Normal View History

using HermesSocketLibrary.Socket.Data;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Socket.Handlers
{
public class HeartbeatHandler : ISocketHandler
{
public int OperationCode { get; } = 0;
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)
{
sender.LastHearbeatSent = DateTime.UtcNow;
await sender.Send(0, new HeartbeatMessage()
{
DateTime = DateTime.UtcNow,
Respond = false
});
}
}
}
}