Try to add online chat
This commit is contained in:
1 parent
2064ed5abc
commit
5e46032547
19 files changed
+520
-16
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,28 @@
|
||||
package site.easystartup.web.chat.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import site.easystartup.web.chat.domain.model.ChatMessage;
|
||||
import site.easystartup.web.chat.domain.model.ChatNotification;
|
||||
|
||||
@Controller
|
||||
public class ChatController {
|
||||
@MessageMapping("/chat.sendMessage")
|
||||
@SendTo("/topic/public")
|
||||
public ChatMessage sendMessage(@Payload ChatMessage chatMessage) {
|
||||
return chatMessage;
|
||||
}
|
||||
@MessageMapping("/chat.addUser")
|
||||
@SendTo("/topic/public")
|
||||
public ChatMessage addUser(@Payload ChatMessage chatMessage,
|
||||
SimpMessageHeaderAccessor headerAccessor) {
|
||||
// Add username in web socket session
|
||||
headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
|
||||
return chatMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package site.easystartup.web.chat.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/ws").withSockJS();
|
||||
}
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||
registry.setApplicationDestinationPrefixes("/app");
|
||||
registry.enableSimpleBroker("/topic");
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,15 @@
|
||||
package site.easystartup.web.chat.domain.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ChatMessage {
|
||||
private MessageType type;
|
||||
private String content;
|
||||
private String sender;
|
||||
public enum MessageType {
|
||||
CHAT,
|
||||
JOIN,
|
||||
LEAVE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package site.easystartup.web.chat.domain.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ChatNotification {
|
||||
private String id;
|
||||
private String senderName;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package site.easystartup.web.chat.domain.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ChatRoom {
|
||||
private String id;
|
||||
private String chatId;
|
||||
private String senderId;
|
||||
private String recipientId;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package site.easystartup.web.chat.domain.model;
|
||||
|
||||
public enum MessageStatus {
|
||||
RECEIVED, DELIVERED
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package site.easystartup.web.chat.event;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.messaging.SessionConnectedEvent;
|
||||
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
|
||||
import site.easystartup.web.chat.domain.model.ChatMessage;
|
||||
|
||||
@Component
|
||||
public class WebSocketEventListener {
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketEventListener.class);
|
||||
@Autowired
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
@EventListener
|
||||
public void handleWebSocketConnectListener(SessionConnectedEvent event) {
|
||||
logger.info("Received a new web socket connection");
|
||||
}
|
||||
@EventListener
|
||||
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
|
||||
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
|
||||
String username = (String) headerAccessor.getSessionAttributes().get("username");
|
||||
if(username != null) {
|
||||
logger.info("User Disconnected : " + username);
|
||||
ChatMessage chatMessage = new ChatMessage();
|
||||
chatMessage.setType(ChatMessage.MessageType.LEAVE);
|
||||
chatMessage.setSender(username);
|
||||
messagingTemplate.convertAndSend("/topic/public", chatMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,7 @@ public class DiscussionMessageRequest {
|
||||
@Size(min = 3, max = 1024, message = "Text should be no less 3 and no more 1024 signs")
|
||||
@NotNull
|
||||
private String text;
|
||||
@NotNull
|
||||
private String sender;
|
||||
@NotNull
|
||||
private Date date;
|
||||
private boolean edited;
|
||||
private long discussion_id;
|
||||
|
||||
@@ -17,16 +17,12 @@ public class DiscussionRequest {
|
||||
@Size(min = 3, max = 100, message = "Title should be no less 3 and no more 100 signs")
|
||||
@NotNull
|
||||
private String title;
|
||||
@NotNull
|
||||
private String author;
|
||||
@NotNull
|
||||
private Date date;
|
||||
@NotNull
|
||||
private Date last_update;
|
||||
private boolean pinned;
|
||||
@NotNull
|
||||
private DiscussionStatus status;
|
||||
private long topic;
|
||||
@NotNull
|
||||
private long topic;
|
||||
private List<Long> messages;
|
||||
}
|
||||
@@ -14,17 +14,12 @@ public class DiscussionDto {
|
||||
@Size(min = 3, max = 100, message = "Title should be no less 3 and no more 100 signs")
|
||||
@NotNull
|
||||
private String title;
|
||||
@NotNull
|
||||
private String author;
|
||||
@NotNull
|
||||
private Date date;
|
||||
@NotNull
|
||||
private Date last_update;
|
||||
private boolean pinned;
|
||||
@NotNull
|
||||
private DiscussionStatus status;
|
||||
private long topic;
|
||||
@NotNull
|
||||
private List<Long> messages;
|
||||
|
||||
}
|
||||
@@ -12,9 +12,7 @@ public class DiscussionMessageDto {
|
||||
@Size(min = 3, max = 1024, message = "Text should be no less 3 and no more 1024 signs")
|
||||
@NotNull
|
||||
private String text;
|
||||
@NotNull
|
||||
private String sender;
|
||||
@NotNull
|
||||
private Date date;
|
||||
private boolean edited;
|
||||
private long discussion_id;
|
||||
|
||||
@@ -12,10 +12,8 @@ public class TopicDto {
|
||||
@Size(min = 3, max = 100, message = "Title should be no less 3 and no more 100 signs")
|
||||
@NotNull
|
||||
private String title;
|
||||
@NotNull
|
||||
private String author;
|
||||
private int size;
|
||||
private String description;
|
||||
@NotNull
|
||||
private Date last_update;
|
||||
}
|
||||
Reference in new issue
Block a user