Skip to content

Commit 30f2b7c

Browse files
committed
add spring websocket copy-paste example; separate client and server
0 parents  commit 30f2b7c

File tree

15 files changed

+342
-0
lines changed

15 files changed

+342
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
target/
3+
*.iml

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
chat server websocket
2+
----------------------
3+

Diff for: chat-server-api/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
chat server with WS
2+
-------------------
3+
4+
```
5+
mvn spring-boot:run
6+
```

Diff for: chat-server-api/build.sbt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name := "chat-server-spring-websocket"
2+
3+
version := "0.1"
4+
5+
scalaVersion := "2.12.5"
6+
7+
libraryDependencies += "org.springframework.boot" % "spring-boot-starter-websocket" % "2.0.1.RELEASE"
8+
libraryDependencies += "org.webjars" % "webjars-locator-core" % "0.35"
9+
libraryDependencies += "org.webjars.npm" % "sockjs-client" % "1.1.4"
10+
libraryDependencies += "org.webjars" % "stomp-websocket" % "2.3.3"
11+
libraryDependencies += "org.webjars" % "bootstrap" % "3.3.5"
12+
libraryDependencies += "org.webjars" % "jquery" % "3.1.0"
13+
14+
resolvers += "mvn central" at "http://central.maven.org/maven2/"
15+
resolvers += "sonatype releases" at "https://oss.sonatype.org/content/repositories/releases"

Diff for: chat-server-api/pom.xml

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.springframework</groupId>
7+
<artifactId>chat-server-api</artifactId>
8+
<version>0.1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-websocket</artifactId>
15+
<version>2.0.1.RELEASE</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>org.webjars</groupId>
20+
<artifactId>webjars-locator-core</artifactId>
21+
<version>0.35</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.webjars</groupId>
26+
<artifactId>sockjs-client</artifactId>
27+
<version>1.0.2</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.webjars</groupId>
31+
<artifactId>stomp-websocket</artifactId>
32+
<version>2.3.3</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.webjars</groupId>
36+
<artifactId>bootstrap</artifactId>
37+
<version>3.3.7</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.webjars</groupId>
41+
<artifactId>jquery</artifactId>
42+
<version>3.1.0</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.fasterxml.jackson.core</groupId>
47+
<artifactId>jackson-annotations</artifactId>
48+
<version>${jackson.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.fasterxml.jackson.core</groupId>
52+
<artifactId>jackson-core</artifactId>
53+
<version>${jackson.version}</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-test</artifactId>
59+
<version>2.0.1.RELEASE</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<repositories>
65+
<repository>
66+
<id>mavencentral</id>
67+
<url>http://central.maven.org/maven2/</url>
68+
</repository>
69+
</repositories>
70+
71+
<properties>
72+
<java.version>1.8</java.version>
73+
<jackson.version>2.9.5</jackson.version>
74+
</properties>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
<version>2.0.1.RELEASE</version>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
86+
<pluginRepositories>
87+
<pluginRepository>
88+
<id>spring-snapshots</id>
89+
<url>https://repo.spring.io/snapshot</url>
90+
</pluginRepository>
91+
<pluginRepository>
92+
<id>spring-milestones</id>
93+
<url>https://repo.spring.io/milestone</url>
94+
</pluginRepository>
95+
</pluginRepositories>
96+
</project>

Diff for: chat-server-api/project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 0.13.17

Diff for: chat-server-api/project/plugins.sbt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.chat.server;
2+
3+
import com.chat.server.schema.ChatRequest;
4+
import com.chat.server.schema.ChatResponse;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.messaging.handler.annotation.MessageMapping;
8+
import org.springframework.messaging.handler.annotation.SendTo;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.util.HtmlUtils;
12+
13+
@Controller
14+
public class ChatEndpoint {
15+
16+
private static Logger logger = LoggerFactory.getLogger(ChatEndpoint.class);
17+
18+
@RequestMapping("/heartbeat")
19+
public String heartbeat() {
20+
return "ChatServer";
21+
}
22+
23+
@MessageMapping("/hello")
24+
@SendTo("/topic/greetings")
25+
public ChatResponse chat(ChatRequest req) throws Exception {
26+
logger.info("received chat request: " + req);
27+
Thread.sleep(1000); // simulated delay
28+
return new ChatResponse("Hello, " + HtmlUtils.htmlEscape(req.getUtterance()) + "!");
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.chat.server;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ChatServer {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ChatServer.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chat.server.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
5+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
6+
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
7+
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
8+
9+
@Configuration
10+
@EnableWebSocketMessageBroker
11+
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
12+
13+
public void configureMessageBroker(MessageBrokerRegistry config) {
14+
config.enableSimpleBroker("/topic");
15+
config.setApplicationDestinationPrefixes("/app");
16+
}
17+
18+
public void registerStompEndpoints(StompEndpointRegistry registry) {
19+
registry.addEndpoint("/gs-guide-websocket").withSockJS();
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.chat.server.schema;
2+
3+
public class ChatRequest {
4+
private String utterance;
5+
6+
public ChatRequest() {
7+
}
8+
9+
public ChatRequest(String utterance) {
10+
this.utterance = utterance;
11+
}
12+
13+
public String getUtterance() {
14+
return utterance;
15+
}
16+
17+
public void setUtterance(String utterance) {
18+
this.utterance = utterance;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.chat.server.schema;
2+
3+
public class ChatResponse {
4+
private String fulfillmentText;
5+
6+
public ChatResponse() {
7+
}
8+
9+
public ChatResponse(String fulfillmentText) {
10+
this.fulfillmentText = fulfillmentText;
11+
}
12+
13+
public String getFulfillmentText() {
14+
return fulfillmentText;
15+
}
16+
}

Diff for: chat-server-api/src/main/resources/static/app.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var stompClient = null;
2+
3+
function setConnected(connected) {
4+
$("#connect").prop("disabled", connected);
5+
$("#disconnect").prop("disabled", !connected);
6+
if (connected) {
7+
$("#conversation").show();
8+
}
9+
else {
10+
$("#conversation").hide();
11+
}
12+
$("#greetings").html("");
13+
}
14+
15+
function connect() {
16+
var socket = new SockJS('/gs-guide-websocket');
17+
stompClient = Stomp.over(socket);
18+
stompClient.connect({}, function (frame) {
19+
setConnected(true);
20+
console.log('Connected: ' + frame);
21+
stompClient.subscribe('/topic/greetings', function (greeting) {
22+
showGreeting(JSON.parse(greeting.body).fulfillmentText);
23+
});
24+
});
25+
}
26+
27+
function disconnect() {
28+
if (stompClient !== null) {
29+
stompClient.disconnect();
30+
}
31+
setConnected(false);
32+
console.log("Disconnected");
33+
}
34+
35+
function sendUtterance() {
36+
stompClient.send("/app/hello", {}, JSON.stringify({'utterance': $("#utterance").val()}));
37+
}
38+
39+
function showGreeting(message) {
40+
$("#greetings").append("<tr><td>" + message + "</td></tr>");
41+
}
42+
43+
$(function () {
44+
$("form").on('submit', function (e) {
45+
e.preventDefault();
46+
});
47+
$( "#connect" ).click(function() { connect(); });
48+
$( "#disconnect" ).click(function() { disconnect(); });
49+
$( "#send" ).click(function() { sendUtterance(); });
50+
});

Diff for: chat-server-api/src/main/resources/static/index.html

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello WebSocket</title>
5+
<link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
6+
<link href="/main.css" rel="stylesheet">
7+
<script src="/webjars/jquery/jquery.min.js"></script>
8+
<script src="/webjars/sockjs-client/sockjs.min.js"></script>
9+
<script src="/webjars/stomp-websocket/stomp.min.js"></script>
10+
<script src="/app.js"></script>
11+
</head>
12+
<body>
13+
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
14+
enabled. Please enable
15+
Javascript and reload this page!</h2></noscript>
16+
<div id="main-content" class="container">
17+
<div class="row">
18+
<div class="col-md-6">
19+
<form class="form-inline">
20+
<div class="form-group">
21+
<label for="connect">Chat init:</label>
22+
<button id="connect" class="btn btn-default" type="submit">Initialise a connection </button>
23+
<button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
24+
</button>
25+
</div>
26+
</form>
27+
</div>
28+
<div class="col-md-6">
29+
<form class="form-inline">
30+
<div class="form-group">
31+
<label for="utterance">What is your question?</label>
32+
<input type="text" id="utterance" class="form-control" placeholder="Your utterance here.">
33+
</div>
34+
<button id="send" class="btn btn-default" type="submit">Ask</button>
35+
</form>
36+
</div>
37+
</div>
38+
<div class="row">
39+
<div class="col-md-12">
40+
<table id="conversation" class="table table-striped">
41+
<thead>
42+
<tr>
43+
<th>Responses</th>
44+
</tr>
45+
</thead>
46+
<tbody id="greetings">
47+
</tbody>
48+
</table>
49+
</div>
50+
</div>
51+
</div>
52+
</body>
53+
</html>

Diff for: chat-server-api/src/main/resources/static/main.css

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
body {
2+
background-color: #f5f5f5;
3+
}
4+
5+
#main-content {
6+
max-width: 940px;
7+
padding: 2em 3em;
8+
margin: 0 auto 20px;
9+
background-color: #fff;
10+
border: 1px solid #e5e5e5;
11+
-webkit-border-radius: 5px;
12+
-moz-border-radius: 5px;
13+
border-radius: 5px;
14+
}

0 commit comments

Comments
 (0)