From 56fd5e8c891a7e9fdfcbcf7659c27f8d1382ece9 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 5 Aug 2020 14:22:32 +0530 Subject: [PATCH] initial websocket commit --- pom.xml | 20 ++--- .../org/ros/chatto/websocket/Message.java | 12 +++ .../ros/chatto/websocket/WebSocketConfig.java | 24 ++++++ .../chatto/websocket/WebSocketController.java | 29 +++++++ src/main/resources/templates/ws.html | 85 +++++++++++++++++++ 5 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/ros/chatto/websocket/Message.java create mode 100644 src/main/java/org/ros/chatto/websocket/WebSocketConfig.java create mode 100644 src/main/java/org/ros/chatto/websocket/WebSocketController.java create mode 100644 src/main/resources/templates/ws.html diff --git a/pom.xml b/pom.xml index 1f9c820..f1dd3a1 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ A self hosted minimal E2E chat application 11 - + @@ -60,11 +60,6 @@ spring-security-test test - org.projectlombok lombok @@ -108,6 +103,11 @@ Either.java 2.2.0 + + org.springframework.boot + spring-boot-starter-websocket + compile + @@ -137,14 +137,6 @@ org.flywaydb flyway-maven-plugin 5.2.4 - ${spring.datasource.driverClassName} jdbc:mysql://${chatto.datasource.url}:${chatto.datasource.port}/${chatto.datasource.database-name} diff --git a/src/main/java/org/ros/chatto/websocket/Message.java b/src/main/java/org/ros/chatto/websocket/Message.java new file mode 100644 index 0000000..ae492e1 --- /dev/null +++ b/src/main/java/org/ros/chatto/websocket/Message.java @@ -0,0 +1,12 @@ +package org.ros.chatto.websocket; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class Message { + private String from; + private String to; + private String text; +} \ No newline at end of file diff --git a/src/main/java/org/ros/chatto/websocket/WebSocketConfig.java b/src/main/java/org/ros/chatto/websocket/WebSocketConfig.java new file mode 100644 index 0000000..66550d7 --- /dev/null +++ b/src/main/java/org/ros/chatto/websocket/WebSocketConfig.java @@ -0,0 +1,24 @@ +package org.ros.chatto.websocket; + +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 configureMessageBroker(MessageBrokerRegistry config) { + config.enableSimpleBroker("/topic", "/queue"); + config.setApplicationDestinationPrefixes("/app"); + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/chat"); + registry.addEndpoint("/chat").withSockJS(); + } +} \ No newline at end of file diff --git a/src/main/java/org/ros/chatto/websocket/WebSocketController.java b/src/main/java/org/ros/chatto/websocket/WebSocketController.java new file mode 100644 index 0000000..6dafadc --- /dev/null +++ b/src/main/java/org/ros/chatto/websocket/WebSocketController.java @@ -0,0 +1,29 @@ +package org.ros.chatto.websocket; + +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.web.bind.annotation.GetMapping; + +import lombok.RequiredArgsConstructor; + +@Controller +@RequiredArgsConstructor +public class WebSocketController { + + private final SimpMessagingTemplate smt; + + @MessageMapping("/chat") + // @SendTo("/topic/messages") + // @SendToUser("/queue/reply") + public void send(@Payload final Message message) throws Exception { + smt.convertAndSendToUser(message.getTo(), "/queue/reply", message); + // return message; + } + + @GetMapping("/ws") + public String wsPage() { + return "ws"; + } +} \ No newline at end of file diff --git a/src/main/resources/templates/ws.html b/src/main/resources/templates/ws.html new file mode 100644 index 0000000..e3e7436 --- /dev/null +++ b/src/main/resources/templates/ws.html @@ -0,0 +1,85 @@ + + + + Chat WebSocket + + + + + + +
+
+ +
+
+ +
+
+
+ + +
+
+
+ + +

+
+
+ + + + \ No newline at end of file