package com.changhu.support.ws.handler;

import com.alibaba.fastjson2.JSON;
import com.changhu.support.ws.enums.WsMsgType;
import com.changhu.support.ws.pojo.dto.WsMsgDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.time.LocalDateTime;

/**
 * author: luozhun
 * desc: HeartbeatMessageHandler
 * createTime: 2023/9/5 14:34
 */
@Slf4j
public class HeartbeatMessageHandler extends BaseMessageHandler {

    public HeartbeatMessageHandler(WebSocketSession session, WsMsgDTO<String> msg) {
        super(session, msg);
    }

    @Override
    public void handlerMessage() {
        try {
            WsMsgDTO<String> stringWsMsgDTO = new WsMsgDTO<>();
            stringWsMsgDTO.setMsgType(WsMsgType.HEARTBEAT);
            stringWsMsgDTO.setData("pong");
            stringWsMsgDTO.setDate(LocalDateTime.now());
            this.session.sendMessage(new TextMessage(JSON.toJSONString(stringWsMsgDTO)));
        } catch (Exception e) {
            log.info("心跳检测失败:{}", e.getMessage());
        }
    }
}
