package cn.sikey.websocket.websocket;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.Duration;
import java.time.Instant;

/**
 * @Author: nelson
 * @Date: 2025/5/29
 * @Description: 连接
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class Connection {
    /**
     * 连接用户ID
     */
    private String id;

    /**
     * WebSocket Key
     */
    private String websocketKey;

    /**
     * 上次心跳时间
     */
    private Instant lastHeartbeat;


    /**
     * 是否已经死亡
     *
     * @param heartbeatTimeout 心跳超时时间
     * @return 是否已经死亡
     */
    synchronized public boolean isDead(Duration heartbeatTimeout) {
        return lastHeartbeat.plus(heartbeatTimeout).isBefore(Instant.now());
    }
}
