From 7852a3ec2b545cd27844c30e580cad8556ac6896 Mon Sep 17 00:00:00 2001 From: Philippe Soares Date: Sun, 25 Oct 2020 23:05:27 -0400 Subject: [PATCH 1/5] Fix for issue #10204. --- .../chat.html => resources/public/index.html} | 50 +++++++++---------- .../public}/js/sockjs-0.3.4.js | 0 .../public}/js/stomp.js | 0 .../public}/js/webSocketSendToUserApp.js | 0 4 files changed, 25 insertions(+), 25 deletions(-) rename spring-websockets/src/main/{webapp/resources/chat.html => resources/public/index.html} (88%) rename spring-websockets/src/main/{webapp/resources => resources/public}/js/sockjs-0.3.4.js (100%) rename spring-websockets/src/main/{webapp/resources => resources/public}/js/stomp.js (100%) rename spring-websockets/src/main/{webapp/resources => resources/public}/js/webSocketSendToUserApp.js (100%) diff --git a/spring-websockets/src/main/webapp/resources/chat.html b/spring-websockets/src/main/resources/public/index.html similarity index 88% rename from spring-websockets/src/main/webapp/resources/chat.html rename to spring-websockets/src/main/resources/public/index.html index 17c8494dd8..f52cca34d1 100644 --- a/spring-websockets/src/main/webapp/resources/chat.html +++ b/spring-websockets/src/main/resources/public/index.html @@ -1,73 +1,73 @@ Chat WebSocket - + - + - + - +
- - + +
@@ -85,4 +85,4 @@
- \ No newline at end of file + diff --git a/spring-websockets/src/main/webapp/resources/js/sockjs-0.3.4.js b/spring-websockets/src/main/resources/public/js/sockjs-0.3.4.js similarity index 100% rename from spring-websockets/src/main/webapp/resources/js/sockjs-0.3.4.js rename to spring-websockets/src/main/resources/public/js/sockjs-0.3.4.js diff --git a/spring-websockets/src/main/webapp/resources/js/stomp.js b/spring-websockets/src/main/resources/public/js/stomp.js similarity index 100% rename from spring-websockets/src/main/webapp/resources/js/stomp.js rename to spring-websockets/src/main/resources/public/js/stomp.js diff --git a/spring-websockets/src/main/webapp/resources/js/webSocketSendToUserApp.js b/spring-websockets/src/main/resources/public/js/webSocketSendToUserApp.js similarity index 100% rename from spring-websockets/src/main/webapp/resources/js/webSocketSendToUserApp.js rename to spring-websockets/src/main/resources/public/js/webSocketSendToUserApp.js From 6dab1d510f3965233cc54423f50a2364115acd46 Mon Sep 17 00:00:00 2001 From: Philippe Soares Date: Sun, 25 Oct 2020 23:28:41 -0400 Subject: [PATCH 2/5] Fix for issue #10204. --- .../src/main/resources/public/index.html | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/spring-websockets/src/main/resources/public/index.html b/spring-websockets/src/main/resources/public/index.html index f52cca34d1..d507e186d4 100644 --- a/spring-websockets/src/main/resources/public/index.html +++ b/spring-websockets/src/main/resources/public/index.html @@ -1,73 +1,73 @@ Chat WebSocket - + - + - + - +
- - + +
@@ -85,4 +85,4 @@
- + \ No newline at end of file From 18954efcee89355109476e9a43bd11e8ba3b8be1 Mon Sep 17 00:00:00 2001 From: Philippe Soares Date: Sun, 25 Oct 2020 23:52:08 -0400 Subject: [PATCH 3/5] Added push messages using the @Scheduled annotation. --- spring-websockets/pom.xml | 5 +++ .../main/java/com/baeldung/SpringBootApp.java | 2 ++ .../websockets/ScheduledPushMessages.java | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java diff --git a/spring-websockets/pom.xml b/spring-websockets/pom.xml index ddfd512476..8f24962185 100644 --- a/spring-websockets/pom.xml +++ b/spring-websockets/pom.xml @@ -18,6 +18,11 @@ org.springframework.boot spring-boot-starter-websocket + + com.github.javafaker + javafaker + 1.0.2 + com.google.code.gson gson diff --git a/spring-websockets/src/main/java/com/baeldung/SpringBootApp.java b/spring-websockets/src/main/java/com/baeldung/SpringBootApp.java index ea2a461dfc..3a98746748 100644 --- a/spring-websockets/src/main/java/com/baeldung/SpringBootApp.java +++ b/spring-websockets/src/main/java/com/baeldung/SpringBootApp.java @@ -3,8 +3,10 @@ package com.baeldung; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class SpringBootApp extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringBootApp.class, args); diff --git a/spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java b/spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java new file mode 100644 index 0000000000..3e27d840d9 --- /dev/null +++ b/spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java @@ -0,0 +1,31 @@ +package com.baeldung.websockets; + + +import com.github.javafaker.Faker; +import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Controller; + +import java.text.SimpleDateFormat; +import java.util.Date; + +@Controller +public class ScheduledPushMessages { + + private final SimpMessagingTemplate simpMessagingTemplate; + + private final Faker faker; + + public ScheduledPushMessages(SimpMessagingTemplate simpMessagingTemplate) { + this.simpMessagingTemplate = simpMessagingTemplate; + faker = new Faker(); + } + + @Scheduled(fixedRate = 5000) + public void sendMessage() { + final String time = new SimpleDateFormat("HH:mm").format(new Date()); + simpMessagingTemplate.convertAndSend("/topic/messages", + new OutputMessage("Chuck Norris", faker.chuckNorris().fact(), time)); + } + +} From 66fff973aeab0792e2fcf814b1bca34104d42da1 Mon Sep 17 00:00:00 2001 From: Philippe Soares Date: Mon, 26 Oct 2020 00:04:15 -0400 Subject: [PATCH 4/5] Added push messages using an interval Flux. --- spring-websockets/pom.xml | 4 +++ .../ReactiveScheduledPushMessages.java | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java diff --git a/spring-websockets/pom.xml b/spring-websockets/pom.xml index 8f24962185..d2a32a8eb6 100644 --- a/spring-websockets/pom.xml +++ b/spring-websockets/pom.xml @@ -18,6 +18,10 @@ org.springframework.boot spring-boot-starter-websocket + + io.projectreactor + reactor-core + com.github.javafaker javafaker diff --git a/spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java b/spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java new file mode 100644 index 0000000000..cfaf981d96 --- /dev/null +++ b/spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java @@ -0,0 +1,32 @@ +package com.baeldung.websockets; + +import com.github.javafaker.Faker; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Flux; + +import java.text.SimpleDateFormat; +import java.time.Duration; +import java.util.Date; + +@Service +public class ReactiveScheduledPushMessages implements InitializingBean { + + private final SimpMessagingTemplate simpMessagingTemplate; + + private final Faker faker; + + public ReactiveScheduledPushMessages(SimpMessagingTemplate simpMessagingTemplate) { + this.simpMessagingTemplate = simpMessagingTemplate; + this.faker = new Faker(); + } + + @Override + public void afterPropertiesSet() throws Exception { + Flux.interval(Duration.ofSeconds(4L)) + .map((n) -> new OutputMessage(faker.backToTheFuture().character(), faker.backToTheFuture().quote(), + new SimpleDateFormat("HH:mm").format(new Date()))) + .subscribe(message -> simpMessagingTemplate.convertAndSend("/topic/messages", message)); + } +} From 6866b51d52b8edc4ebceb77bcbe51c8b7ae8d562 Mon Sep 17 00:00:00 2001 From: Philippe Soares Date: Sun, 8 Nov 2020 11:38:17 -0500 Subject: [PATCH 5/5] Put html and js resources back to their original location. Split backend code so it doesn't modify existing examples. Added a bots.html page for the server push version. --- .../baeldung/websockets/BotsController.java | 21 +++++ .../ReactiveScheduledPushMessages.java | 2 +- .../websockets/ScheduledPushMessages.java | 5 +- .../baeldung/websockets/WebSocketConfig.java | 2 + spring-websockets/src/main/webapp/bots.html | 88 +++++++++++++++++++ .../resources}/js/sockjs-0.3.4.js | 0 .../public => webapp/resources}/js/stomp.js | 0 .../resources}/js/webSocketSendToUserApp.js | 0 8 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 spring-websockets/src/main/java/com/baeldung/websockets/BotsController.java create mode 100644 spring-websockets/src/main/webapp/bots.html rename spring-websockets/src/main/{resources/public => webapp/resources}/js/sockjs-0.3.4.js (100%) rename spring-websockets/src/main/{resources/public => webapp/resources}/js/stomp.js (100%) rename spring-websockets/src/main/{resources/public => webapp/resources}/js/webSocketSendToUserApp.js (100%) diff --git a/spring-websockets/src/main/java/com/baeldung/websockets/BotsController.java b/spring-websockets/src/main/java/com/baeldung/websockets/BotsController.java new file mode 100644 index 0000000000..3f268f3794 --- /dev/null +++ b/spring-websockets/src/main/java/com/baeldung/websockets/BotsController.java @@ -0,0 +1,21 @@ +package com.baeldung.websockets; + +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.stereotype.Controller; + +import java.text.SimpleDateFormat; +import java.util.Date; + +@Controller +public class BotsController { + + @MessageMapping("/chatwithbots") + @SendTo("/topic/pushmessages") + public OutputMessage send(final Message message) throws Exception { + + final String time = new SimpleDateFormat("HH:mm").format(new Date()); + return new OutputMessage(message.getFrom(), message.getText(), time); + } + +} diff --git a/spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java b/spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java index cfaf981d96..36b1b886fc 100644 --- a/spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java +++ b/spring-websockets/src/main/java/com/baeldung/websockets/ReactiveScheduledPushMessages.java @@ -27,6 +27,6 @@ public class ReactiveScheduledPushMessages implements InitializingBean { Flux.interval(Duration.ofSeconds(4L)) .map((n) -> new OutputMessage(faker.backToTheFuture().character(), faker.backToTheFuture().quote(), new SimpleDateFormat("HH:mm").format(new Date()))) - .subscribe(message -> simpMessagingTemplate.convertAndSend("/topic/messages", message)); + .subscribe(message -> simpMessagingTemplate.convertAndSend("/topic/pushmessages", message)); } } diff --git a/spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java b/spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java index 3e27d840d9..2468b69713 100644 --- a/spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java +++ b/spring-websockets/src/main/java/com/baeldung/websockets/ScheduledPushMessages.java @@ -5,11 +5,12 @@ import com.github.javafaker.Faker; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Controller; +import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; -@Controller +@Service public class ScheduledPushMessages { private final SimpMessagingTemplate simpMessagingTemplate; @@ -24,7 +25,7 @@ public class ScheduledPushMessages { @Scheduled(fixedRate = 5000) public void sendMessage() { final String time = new SimpleDateFormat("HH:mm").format(new Date()); - simpMessagingTemplate.convertAndSend("/topic/messages", + simpMessagingTemplate.convertAndSend("/topic/pushmessages", new OutputMessage("Chuck Norris", faker.chuckNorris().fact(), time)); } diff --git a/spring-websockets/src/main/java/com/baeldung/websockets/WebSocketConfig.java b/spring-websockets/src/main/java/com/baeldung/websockets/WebSocketConfig.java index 7b53dbc3f3..6179ec9c0d 100644 --- a/spring-websockets/src/main/java/com/baeldung/websockets/WebSocketConfig.java +++ b/spring-websockets/src/main/java/com/baeldung/websockets/WebSocketConfig.java @@ -20,6 +20,8 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { public void registerStompEndpoints(final StompEndpointRegistry registry) { registry.addEndpoint("/chat"); registry.addEndpoint("/chat").withSockJS(); + registry.addEndpoint("/chatwithbots"); + registry.addEndpoint("/chatwithbots").withSockJS(); } } \ No newline at end of file diff --git a/spring-websockets/src/main/webapp/bots.html b/spring-websockets/src/main/webapp/bots.html new file mode 100644 index 0000000000..38570d420c --- /dev/null +++ b/spring-websockets/src/main/webapp/bots.html @@ -0,0 +1,88 @@ + + + Chat WebSocket + + + + + + + + + + +
+ + +
+ +
+
+
+ + +
+
+
+ + +

+
+
+ + + \ No newline at end of file diff --git a/spring-websockets/src/main/resources/public/js/sockjs-0.3.4.js b/spring-websockets/src/main/webapp/resources/js/sockjs-0.3.4.js similarity index 100% rename from spring-websockets/src/main/resources/public/js/sockjs-0.3.4.js rename to spring-websockets/src/main/webapp/resources/js/sockjs-0.3.4.js diff --git a/spring-websockets/src/main/resources/public/js/stomp.js b/spring-websockets/src/main/webapp/resources/js/stomp.js similarity index 100% rename from spring-websockets/src/main/resources/public/js/stomp.js rename to spring-websockets/src/main/webapp/resources/js/stomp.js diff --git a/spring-websockets/src/main/resources/public/js/webSocketSendToUserApp.js b/spring-websockets/src/main/webapp/resources/js/webSocketSendToUserApp.js similarity index 100% rename from spring-websockets/src/main/resources/public/js/webSocketSendToUserApp.js rename to spring-websockets/src/main/webapp/resources/js/webSocketSendToUserApp.js