Adding spring websockets demo application.
parent
2aaa6c9882
commit
f107c2cf1f
|
@ -1,2 +1,3 @@
|
|||
rootProject.name = 'spring-showcase'
|
||||
include ':spring-showcase-web'
|
||||
include ':spring-websockets-demo1'
|
||||
|
|
|
@ -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/
|
|
@ -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()
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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()));
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -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
|
||||
}));
|
||||
});
|
||||
});
|
|
@ -0,0 +1,42 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Showcas for simple websocket messaging</title>
|
||||
<link rel="stylesheet" href="/webjars/bulma/0.9.1/css/bulma.min.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="field">
|
||||
<label class="label">Message</label>
|
||||
<div class="control">
|
||||
<input id="message-content" type="text" class="input" placeholder="Message"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field is-grouped">
|
||||
<div class="control">
|
||||
<button id="submit-message" class="button is-link">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<table id="messages" class="table is-striped is-fullwidth">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Time</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script src="/webjars/sockjs-client/1.5.0/dist/sockjs.js"></script>
|
||||
<script src="/webjars/stomp-websocket/2.3.3/stomp.js"></script>
|
||||
<script type="application/javascript" src="/app.js"></script>
|
||||
</body>
|
||||
</html>
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue