Skip to content

Commit

Permalink
add bean, config, controller logic for private msg
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 17, 2023
1 parent 3cf06ad commit a6b94e8
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 12 deletions.
6 changes: 6 additions & 0 deletions springChatRoom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<scope>test</scope>
</dependency>

<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yen.springChatRoom.model;
package com.yen.springChatRoom.bean;

public class ChatMessage {
private MessageType type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.yen.springChatRoom.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Message {

private String sender;
private String content;
private String type;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.yen.springChatRoom.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
Expand Down Expand Up @@ -28,7 +30,16 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
public void configureMessageBroker(MessageBrokerRegistry registry) {

//WebSocketMessageBrokerConfigurer.super.configureMessageBroker(registry);
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app"); // (client -> server)
registry.enableSimpleBroker("/topic", "/private"); // (server -> client)
}

//
// @Bean
// public SimpMessagingTemplate messagingTemplate() {
//
// //return new SimpMessagingTemplate(/* your message broker relay, e.g., "/topic" */);
// return new SimpMessagingTemplate("/private/");
// }

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.yen.springChatRoom.controller;

import com.yen.springChatRoom.model.ChatMessage;
import com.yen.springChatRoom.bean.Message;
import com.yen.springChatRoom.bean.ChatMessage;
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.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Set;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ChatController {
Expand All @@ -35,6 +35,9 @@ public class ChatController {
private RedisTemplate<String, String> redisTemplate;
//private RedisTemplate redisTemplate;

@Autowired
private SimpMessagingTemplate simpMessagingTemplate;

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

/**
Expand Down Expand Up @@ -79,4 +82,11 @@ public void addUser(@Payload ChatMessage chatMessage, SimpMessageHeaderAccessor
}
}

// TODO : check @DestinationVariable ?
@RequestMapping("/private/{username}")
public void handlePrivateMessage(@DestinationVariable String username, Message message){

simpMessagingTemplate.convertAndSendToUser(username, "/topic/private", message);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions springChatRoom/src/main/resources/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{},
Expand Down

0 comments on commit a6b94e8

Please sign in to comment.