redisTemplate;
//private RedisTemplate redisTemplate;
+ @Autowired
+ private SimpMessagingTemplate simpMessagingTemplate;
+
private static final Logger LOGGER = LoggerFactory.getLogger(ChatController.class);
/**
@@ -56,6 +64,9 @@ public class ChatController {
@MessageMapping("/chat.sendMessage")
public void sendMessage(@Payload ChatMessage chatMessage){
try{
+
+ // test : save msg to redis
+ redisTemplate.opsForSet().add(msgToAll, JsonUtil.parseObjToJson(chatMessage));
//redisTemplate.convertAndSend(msgToAll, JsonUtil.parseObjToJson(chatMessage)));
redisTemplate.convertAndSend(msgToAll, JsonUtil.parseObjToJson(chatMessage));
}catch (Exception e){
@@ -79,4 +90,17 @@ public void addUser(@Payload ChatMessage chatMessage, SimpMessageHeaderAccessor
}
}
+ // TODO : check @DestinationVariable ?
+ @RequestMapping("/app/private/{username}")
+ public void handlePrivateMessage(@DestinationVariable String username, Message message){
+
+ log.info("handlePrivateMessage : username = " + username + " message = " + message);
+ // save to redis
+
+ // redisTemplate.convertAndSend(userStatus, JsonUtil.parseObjToJson(chatMessage));
+ redisTemplate.opsForSet().add(privateChannel + "." + username, JsonUtil.parseObjToJson(message));
+
+ simpMessagingTemplate.convertAndSendToUser(username, "/topic/private", message);
+ }
+
}
diff --git a/springChatRoom/src/main/java/com/yen/springChatRoom/listener/WebSocketEventListener.java b/springChatRoom/src/main/java/com/yen/springChatRoom/listener/WebSocketEventListener.java
index 72b714d91..5c33d781d 100644
--- a/springChatRoom/src/main/java/com/yen/springChatRoom/listener/WebSocketEventListener.java
+++ b/springChatRoom/src/main/java/com/yen/springChatRoom/listener/WebSocketEventListener.java
@@ -1,7 +1,7 @@
package com.yen.springChatRoom.listener;
import com.yen.springChatRoom.controller.ChatController;
-import com.yen.springChatRoom.model.ChatMessage;
+import com.yen.springChatRoom.bean.ChatMessage;
import com.yen.springChatRoom.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git a/springChatRoom/src/main/java/com/yen/springChatRoom/redis/RedisListenerHandle.java b/springChatRoom/src/main/java/com/yen/springChatRoom/redis/RedisListenerHandle.java
index 266f8f5ae..eb46219dd 100644
--- a/springChatRoom/src/main/java/com/yen/springChatRoom/redis/RedisListenerHandle.java
+++ b/springChatRoom/src/main/java/com/yen/springChatRoom/redis/RedisListenerHandle.java
@@ -1,6 +1,6 @@
package com.yen.springChatRoom.redis;
-import com.yen.springChatRoom.model.ChatMessage;
+import com.yen.springChatRoom.bean.ChatMessage;
import com.yen.springChatRoom.service.ChatService;
import com.yen.springChatRoom.util.JsonUtil;
import org.slf4j.Logger;
diff --git a/springChatRoom/src/main/java/com/yen/springChatRoom/service/ChatService.java b/springChatRoom/src/main/java/com/yen/springChatRoom/service/ChatService.java
index 105f8a736..b692ed38d 100644
--- a/springChatRoom/src/main/java/com/yen/springChatRoom/service/ChatService.java
+++ b/springChatRoom/src/main/java/com/yen/springChatRoom/service/ChatService.java
@@ -1,13 +1,12 @@
package com.yen.springChatRoom.service;
import com.yen.springChatRoom.controller.ChatController;
-import com.yen.springChatRoom.model.ChatMessage;
+import com.yen.springChatRoom.bean.ChatMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
-import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler;
import org.springframework.stereotype.Service;
@Service
diff --git a/springChatRoom/src/main/resources/application.properties b/springChatRoom/src/main/resources/application.properties
index 6f0ab9d97..5d2930acd 100644
--- a/springChatRoom/src/main/resources/application.properties
+++ b/springChatRoom/src/main/resources/application.properties
@@ -18,3 +18,5 @@ server.port=8080
redis.set.onlineUsers = websocket.onlineUsers
redis.channel.msgToAll = websocket.msgToAll
redis.channel.userStatus = websocket.userStatus
+redis.channel.private = websocket.privateMsg
+
diff --git a/springChatRoom/src/main/resources/static/js/main.js b/springChatRoom/src/main/resources/static/js/main.js
index 71d72a5ac..069c605a8 100644
--- a/springChatRoom/src/main/resources/static/js/main.js
+++ b/springChatRoom/src/main/resources/static/js/main.js
@@ -37,9 +37,13 @@ function connect(event) {
function onConnected() {
+
// Subscribe to the Public Topic
stompClient.subscribe('/topic/public', onMessageReceived);
+ // Subscribe to the "/private" destination // TODO : make it general
+ //stompClient.subscribe('/private/user123', onPrivateMessageReceived);
+
// Tell your username to the server
stompClient.send("/app/chat.addUser",
{},
@@ -158,7 +162,6 @@ function updateOnlineUsers(users) {
// Append the username span to the list item
listItem.appendChild(usernameSpan);
-
// Create a "Chat" button
const chatButton = document.createElement('button');
chatButton.textContent = 'Chat';
@@ -167,7 +170,6 @@ function updateOnlineUsers(users) {
// Append the "Chat" button to the list item
listItem.appendChild(chatButton);
-
// Append the list item to the user list
userList.appendChild(listItem);
});
@@ -189,6 +191,8 @@ function startChat(username) {
// Function to send a message from the popup window
popupWindow.sendMessage = function() {
+
+ console.log(">>> popupWindow.sendMessage")
const messageInput = popupWindow.document.getElementById('messageInput');
const chatMessages = popupWindow.document.getElementById('chatMessages');
@@ -197,15 +201,38 @@ function startChat(username) {
// Customize the way messages are displayed in the popup window
chatMessages.innerHTML += 'You: ' + message + '
';
+ // TODO: Fetch and display chat history
+ //fetchChatHistory(username, chatMessages);
+
+ // TODO : implement below in BE
// Add your logic to send the message to the other user
// Example: stompClient.send('/app/private/' + username, {}, JSON.stringify({ sender: 'You', content: message, type: 'CHAT' }));
+ // send msg to BE
+ //stompClient.subscribe('/app/private/' + username, onPrivateMessageReceived);
+ stompClient.subscribe('/app/private/' + username);
+ console.log(">>> send msg to /app/private/" + username + ", message = " + message);
+ stompClient.send('/app/private/' + username, {}, JSON.stringify({ sender: 'You', content: message, type: 'CHAT' }));
+
// Clear the input field
messageInput.value = '';
}
};
}
+// Function to fetch and display chat history
+function fetchChatHistory(username, chatMessages) {
+ fetch('/app/chat/history/' + username)
+ .then(response => response.json())
+ .then(history => {
+ history.forEach(message => {
+ chatMessages.innerHTML += '' + message.sender + ': ' + message.content + '
';
+ });
+ })
+ .catch(error => {
+ console.error('Error fetching chat history: ', error);
+ });
+}
// Call the fetchUserList function to initially populate the user list