package com.sikey.wa04.business.domain.entity.websocket;

import com.sikey.wa04.business.domain.entity.UserId;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.Duration;
import java.time.Instant;
import java.util.Locale;

/**
 * 连接
 *
 * @author luoyangwei
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class Connection {
    /**
     * 连接用户ID
     */
    private UserId id;

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

    /**
     * 语言环境
     * 默认: {@link com.sikey.wa04.common.constants.LocaleConstant#DEFAULT_LANGUAGE}
     */
    private Locale locale;

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


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