From a2014545e1e229414312f00daaec665cf1ff3e79 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Mon, 13 Nov 2023 17:41:56 +0800 Subject: [PATCH] add private msg controller endpoint --- .../controller/ChatController.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/springChatRoom/src/main/java/com/yen/springChatRoom/controller/ChatController.java b/springChatRoom/src/main/java/com/yen/springChatRoom/controller/ChatController.java index 9f08febe6..b4076f8e9 100644 --- a/springChatRoom/src/main/java/com/yen/springChatRoom/controller/ChatController.java +++ b/springChatRoom/src/main/java/com/yen/springChatRoom/controller/ChatController.java @@ -1,12 +1,17 @@ package com.yen.springChatRoom.controller; +import com.yen.springChatRoom.bean.Channel; import com.yen.springChatRoom.bean.ChatMessage; +import com.yen.springChatRoom.bean.User; +import com.yen.springChatRoom.service.ChatService; import com.yen.springChatRoom.util.JsonUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.messaging.handler.annotation.DestinationVariable; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.Payload; +import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.slf4j.Logger; @@ -31,6 +36,12 @@ public class ChatController { private RedisTemplate redisTemplate; //private RedisTemplate redisTemplate; + @Autowired + private ChatService chatService; + + @Autowired + private SimpMessagingTemplate messagingTemplate; + private static final Logger LOGGER = LoggerFactory.getLogger(ChatController.class); /** @@ -72,4 +83,19 @@ public void addUser(@Payload ChatMessage chatMessage, SimpMessageHeaderAccessor } } + /** user - user private msg */ + @MessageMapping("/private/{channelId}") + private void handlePrivateMessage(@DestinationVariable String channelId, ChatMessage chatMessage){ + + Channel channel = new Channel(); + if (channel != null){ + for(User user : channel.getUsers()){ + // TODO : check if can use redisTemplate + // TODO : check what's channel for user-user + //messagingTemplate.convertAndSendToUser(user.getUserName(), "/topic/private/" + channelId, chatMessage); + redisTemplate.convertAndSend("/topic/private/" + channelId, chatMessage); + } + } + } + }