package com.emotion.controller; import com.emotion.service.AiChatService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * WebSocket控制器 * * @author emotion-museum * @date 2025-07-22 */ @Controller public class WebSocketController { private static final Logger log = LoggerFactory.getLogger(WebSocketController.class); @Autowired private SimpMessagingTemplate messagingTemplate; @Autowired private AiChatService aiChatService; // 已移除旧的WebSocket消息处理方法,使用新的ChatWebSocketController // 已移除旧的用户连接处理方法,使用新的ChatWebSocketController /** * 处理AI聊天消息 */ @MessageMapping("/chat.ai") public void handleAiChat(@Payload Map chatMessage, SimpMessageHeaderAccessor headerAccessor) { log.info("收到AI聊天消息: {}", chatMessage); try { String content = (String) chatMessage.get("content"); String conversationId = (String) chatMessage.get("conversationId"); String userId = (String) chatMessage.get("userId"); // 调用AI服务 String aiReply = aiChatService.sendMessage(conversationId, content, userId); // 构建AI回复消息 Map aiResponse = new HashMap<>(); aiResponse.put("content", aiReply); aiResponse.put("sender", "AI助手"); aiResponse.put("type", "AI_REPLY"); aiResponse.put("conversationId", conversationId); aiResponse.put("timestamp", System.currentTimeMillis()); // 发送给特定用户 if (userId != null) { messagingTemplate.convertAndSendToUser(userId, "/queue/messages", aiResponse); } else { // 发送到公共频道 messagingTemplate.convertAndSend("/topic/conversation/" + conversationId, aiResponse); } } catch (Exception e) { log.error("处理AI聊天失败", e); Map errorResponse = new HashMap<>(); errorResponse.put("content", "AI服务暂时不可用,请稍后再试"); errorResponse.put("sender", "System"); errorResponse.put("type", "ERROR"); errorResponse.put("timestamp", System.currentTimeMillis()); String userId = (String) chatMessage.get("userId"); if (userId != null) { messagingTemplate.convertAndSendToUser(userId, "/queue/messages", errorResponse); } } } /** * 发送系统消息 */ public void sendSystemMessage(String destination, String message) { Map systemMessage = new HashMap<>(); systemMessage.put("content", message); systemMessage.put("sender", "System"); systemMessage.put("type", "SYSTEM"); systemMessage.put("timestamp", System.currentTimeMillis()); messagingTemplate.convertAndSend(destination, systemMessage); } /** * WebSocket状态监控接口 */ @GetMapping("/api/ws/status") @ResponseBody public Map getWebSocketStatus() { Map status = new HashMap<>(); status.put("status", "active"); status.put("timestamp", System.currentTimeMillis()); status.put("endpoint", "/ws/chat"); status.put("protocols", new String[]{"websocket", "sockjs"}); status.put("message", "WebSocket服务正常运行"); return status; } }