Skip to content

Commit

Permalink
add private msg controller endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 13, 2023
1 parent 72af31c commit a201454
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -31,6 +36,12 @@ public class ChatController {
private RedisTemplate<String, String> redisTemplate;
//private RedisTemplate redisTemplate;

@Autowired
private ChatService chatService;

@Autowired
private SimpMessagingTemplate messagingTemplate;

private static final Logger LOGGER = LoggerFactory.getLogger(ChatController.class);

/**
Expand Down Expand Up @@ -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);
}
}
}

}

0 comments on commit a201454

Please sign in to comment.