This commit is contained in:
jiangh277
2025-12-24 14:17:19 +08:00
parent 3eb445291f
commit 4c7d59f87b
89 changed files with 3525 additions and 311 deletions

View File

@@ -0,0 +1,34 @@
package com.timeline.user.ws;
import com.timeline.common.utils.UserContextUtils;
import com.timeline.user.dto.ChatMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Controller;
import java.security.Principal;
@Slf4j
@Controller
public class ChatWsController {
@Autowired
private WsNotifyService wsNotifyService;
@MessageMapping("/chat/send")
public void send(@Payload ChatMessage msg, Principal principal) {
if (principal == null) {
log.warn("未认证用户尝试发送聊天消息");
return;
}
String fromUserId = principal.getName();
msg.setFromUserId(fromUserId);
msg.setFromUsername(UserContextUtils.getCurrentUsername());
msg.setTimestamp(System.currentTimeMillis());
log.info("用户 {}({}) 向 {} 发送消息: {}", fromUserId, msg.getFromUsername(), msg.getToUserId(), msg.getContent());
wsNotifyService.sendChatMessage(msg.getToUserId(), msg);
}
}