35 lines
1.2 KiB
Java
35 lines
1.2 KiB
Java
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);
|
|
}
|
|
}
|
|
|