From f107c2cf1f0527c8bee59c783eea00da7989fe0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sat, 16 Jan 2021 18:36:49 +0100 Subject: [PATCH] Adding spring websockets demo application. --- settings.gradle | 1 + spring-websockets-demo1/.gitignore | 37 ++++++++++++++++ spring-websockets-demo1/build.gradle | 26 ++++++++++++ .../SpringWebsocketsDemo1Application.java | 13 ++++++ .../configs/WebsocketsConfig.java | 22 ++++++++++ .../controllers/WebsocketController.java | 16 +++++++ .../models/Greeting.java | 17 ++++++++ .../models/HelloMessage.java | 13 ++++++ .../src/main/resources/application.properties | 1 + .../src/main/resources/static/app.js | 34 +++++++++++++++ .../src/main/resources/static/index.html | 42 +++++++++++++++++++ ...SpringWebsocketsDemo1ApplicationTests.java | 13 ++++++ 12 files changed, 235 insertions(+) create mode 100644 spring-websockets-demo1/.gitignore create mode 100644 spring-websockets-demo1/build.gradle create mode 100644 spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1Application.java create mode 100644 spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/configs/WebsocketsConfig.java create mode 100644 spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/controllers/WebsocketController.java create mode 100644 spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/Greeting.java create mode 100644 spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/HelloMessage.java create mode 100644 spring-websockets-demo1/src/main/resources/application.properties create mode 100644 spring-websockets-demo1/src/main/resources/static/app.js create mode 100644 spring-websockets-demo1/src/main/resources/static/index.html create mode 100644 spring-websockets-demo1/src/test/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1ApplicationTests.java diff --git a/settings.gradle b/settings.gradle index f86da08..83b612c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ rootProject.name = 'spring-showcase' include ':spring-showcase-web' +include ':spring-websockets-demo1' diff --git a/spring-websockets-demo1/.gitignore b/spring-websockets-demo1/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/spring-websockets-demo1/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/spring-websockets-demo1/build.gradle b/spring-websockets-demo1/build.gradle new file mode 100644 index 0000000..a51b9b8 --- /dev/null +++ b/spring-websockets-demo1/build.gradle @@ -0,0 +1,26 @@ +plugins { + id 'org.springframework.boot' version '2.4.2' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' + id 'java' +} + +group = 'ch.tpolgrabia.demos.springshowcase' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '11' + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-websocket' + implementation 'org.webjars.npm:sockjs-client:1.5.0' + implementation 'org.webjars:stomp-websocket:2.3.3' + implementation 'org.webjars.npm:bulma:0.9.1' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +test { + useJUnitPlatform() +} diff --git a/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1Application.java b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1Application.java new file mode 100644 index 0000000..34bc586 --- /dev/null +++ b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1Application.java @@ -0,0 +1,13 @@ +package ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringWebsocketsDemo1Application { + + public static void main(String[] args) { + SpringApplication.run(SpringWebsocketsDemo1Application.class, args); + } + +} diff --git a/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/configs/WebsocketsConfig.java b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/configs/WebsocketsConfig.java new file mode 100644 index 0000000..879129d --- /dev/null +++ b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/configs/WebsocketsConfig.java @@ -0,0 +1,22 @@ +package ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1.configs; + +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 WebsocketsConfig implements WebSocketMessageBrokerConfigurer { + @Override + public void configureMessageBroker(MessageBrokerRegistry registry) { + registry.enableSimpleBroker("/topic"); + registry.setApplicationDestinationPrefixes("/app"); + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/gs-guide-websocket").withSockJS(); + } +} diff --git a/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/controllers/WebsocketController.java b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/controllers/WebsocketController.java new file mode 100644 index 0000000..1afba31 --- /dev/null +++ b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/controllers/WebsocketController.java @@ -0,0 +1,16 @@ +package ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1.controllers; + +import ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1.models.Greeting; +import ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1.models.HelloMessage; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.stereotype.Controller; + +@Controller +public class WebsocketController { + @MessageMapping("/hello") + @SendTo("/topic/greetings") + public Greeting greeting(HelloMessage message) { + return new Greeting(String.format("Hello %s!!!", message.getName())); + } +} diff --git a/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/Greeting.java b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/Greeting.java new file mode 100644 index 0000000..b1bd52a --- /dev/null +++ b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/Greeting.java @@ -0,0 +1,17 @@ +package ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1.models; + +public class Greeting { + private String content; + + public Greeting(String content) { + this.content = content; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/HelloMessage.java b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/HelloMessage.java new file mode 100644 index 0000000..91ce78b --- /dev/null +++ b/spring-websockets-demo1/src/main/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/models/HelloMessage.java @@ -0,0 +1,13 @@ +package ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1.models; + +public class HelloMessage { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-websockets-demo1/src/main/resources/application.properties b/spring-websockets-demo1/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/spring-websockets-demo1/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/spring-websockets-demo1/src/main/resources/static/app.js b/spring-websockets-demo1/src/main/resources/static/app.js new file mode 100644 index 0000000..8ebc952 --- /dev/null +++ b/spring-websockets-demo1/src/main/resources/static/app.js @@ -0,0 +1,34 @@ +window.addEventListener('DOMContentLoaded', (event) => { + // let sockJs = new SockJS('/gs-guide-websocket'); + let sockJs = new SockJS('/gs-guide-websocket'); + let stompClient = Stomp.over(sockJs); + stompClient.connect('', '', (frame) => { + console.log(`Connected to ${frame}`); + stompClient.subscribe('/topic/greetings', function (greeting) { + console.log(`Got greeting: ${greeting}`); + + let el = document.querySelector("#messages > tbody"); + console.log('Got', el); + let tr = document.createElement('tr'); + let td1 = document.createElement('td'); + let td2 = document.createElement('td'); + let message = JSON.parse(greeting.body); + td1.textContent = new Date().toISOString(); + td2.textContent = message.content; + + tr.append(td1); + tr.append(td2); + el.append(tr); + }); + }); + + document.querySelector('#submit-message') + .addEventListener('click', (e) => { + let messageContentNode = document.querySelector('#message-content'); + let messageValue = messageContentNode.value; + messageContentNode.value = ''; + stompClient.send('/app/hello', {}, JSON.stringify({ + name: messageValue + })); + }); +}); diff --git a/spring-websockets-demo1/src/main/resources/static/index.html b/spring-websockets-demo1/src/main/resources/static/index.html new file mode 100644 index 0000000..ef3d8b7 --- /dev/null +++ b/spring-websockets-demo1/src/main/resources/static/index.html @@ -0,0 +1,42 @@ + + + + + Showcas for simple websocket messaging + + + +
+
+ +
+ +
+
+ +
+
+ +
+
+
+ +
+ + + + + + + + + + +
TimeMessage
+
+ + + + + + diff --git a/spring-websockets-demo1/src/test/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1ApplicationTests.java b/spring-websockets-demo1/src/test/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1ApplicationTests.java new file mode 100644 index 0000000..fd88c03 --- /dev/null +++ b/spring-websockets-demo1/src/test/java/ch/tpolgrabia/demos/springshowcase/springwebsocketsdemo1/SpringWebsocketsDemo1ApplicationTests.java @@ -0,0 +1,13 @@ +package ch.tpolgrabia.demos.springshowcase.springwebsocketsdemo1; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringWebsocketsDemo1ApplicationTests { + + @Test + void contextLoads() { + } + +}