//package com.ruoyi.web.controller.app.websocket;
//
//import com.ruoyi.system.domain.HeartbeatMessage;
//import com.ruoyi.system.domain.HeartbeatResponse;
//import com.ruoyi.system.service.ISysUserOnlineTimeService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.handler.annotation.MessageMapping;
//import org.springframework.messaging.handler.annotation.Payload;
//import org.springframework.messaging.simp.annotation.SendToUser;
//import org.springframework.stereotype.Controller;
//
//@Controller
//public class WebSocketHeartbeatController {
//
//    @Autowired
//    private ISysUserOnlineTimeService userOnlineTimeService;
//
//    @MessageMapping("/heartbeat")
//    @SendToUser("/queue/heartbeat")
//    public HeartbeatResponse handleHeartbeat(@Payload HeartbeatMessage message) {
//        try {
//            if (message == null || message.getUserId() == null) {
//
//                System.out.println("接收到无效的心跳消息");
////                log.error("接收到无效的心跳消息");
//                return new HeartbeatResponse(System.currentTimeMillis(), "无效的心跳消息");
//            }
//
//            // 更新用户在线时长
//            userOnlineTimeService.updateUserHeartbeat(message.getUserId());
//            // 返回心跳响应
//            return new HeartbeatResponse(message.getTimestamp(), "success");
//        } catch (Exception e) {
//            System.out.println("处理心跳消息时发生错误");
////            log.error("处理心跳消息时发生错误", e);
//            return new HeartbeatResponse(System.currentTimeMillis(), "error");
//        }
//    }
//}
//
//
