Skip to content

Commit 4d1dd06

Browse files
committed
Add websocket example
1 parent 70e9d2f commit 4d1dd06

7 files changed

Lines changed: 3021 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.model;
2+
3+
public class Message {
4+
5+
private String text;
6+
7+
public String getText() {
8+
return text;
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.model;
2+
3+
public class OutputMessage {
4+
5+
private String text;
6+
private String time;
7+
8+
public OutputMessage(final String text, final String time) {
9+
this.text = text;
10+
this.time = time;
11+
}
12+
13+
public String getText() {
14+
return text;
15+
}
16+
17+
public String getTime() {
18+
return time;
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.spring.web.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.AbstractWebSocketMessageBrokerConfigurer;
6+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
7+
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
8+
9+
@Configuration
10+
@EnableWebSocketMessageBroker
11+
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
12+
13+
@Override
14+
public void configureMessageBroker(final MessageBrokerRegistry config) {
15+
config.enableSimpleBroker("/topic");
16+
config.setApplicationDestinationPrefixes("/app");
17+
}
18+
19+
@Override
20+
public void registerStompEndpoints(final StompEndpointRegistry registry) {
21+
registry.addEndpoint("/chat").withSockJS();
22+
}
23+
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.web.controller;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
import org.baeldung.model.Message;
7+
import org.baeldung.model.OutputMessage;
8+
import org.springframework.messaging.handler.annotation.MessageMapping;
9+
import org.springframework.messaging.handler.annotation.SendTo;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
14+
@Controller
15+
public class ChatController {
16+
17+
@RequestMapping(value = "/showChat", method = RequestMethod.GET)
18+
public String displayChat() {
19+
return "chat";
20+
}
21+
22+
@MessageMapping("/chat")
23+
@SendTo("/topic/")
24+
public OutputMessage send(final Message message) throws Exception {
25+
26+
final String time = new SimpleDateFormat("HH:mm").format(new Date());
27+
return new OutputMessage(message.getText(), time);
28+
}
29+
30+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
2+
<html>
3+
<head>
4+
<title>Chat WebSocket</title>
5+
6+
<spring:url value="/resources/js/sockjs-0.3.4.js" var="sockjs" />
7+
<spring:url value="/resources/js/stomp.js" var="stomp" />
8+
9+
<script src="${sockjs}"></script>
10+
<script src="${stomp}"></script>
11+
12+
<script type="text/javascript">
13+
14+
var stompClient = null;
15+
16+
function setConnected(connected) {
17+
18+
document.getElementById('connect').disabled = connected;
19+
document.getElementById('disconnect').disabled = !connected;
20+
document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
21+
document.getElementById('response').innerHTML = '';
22+
}
23+
24+
function connect() {
25+
26+
var socket = new SockJS('/spring-mvc-java/chat');
27+
stompClient = Stomp.over(socket);
28+
29+
stompClient.connect({}, function(frame) {
30+
31+
setConnected(true);
32+
console.log('Connected: ' + frame);
33+
stompClient.subscribe('/topic/message', function(messageOutput) {
34+
showMessageOutput(JSON.parse(messageOutput.body));
35+
});
36+
});
37+
}
38+
39+
function disconnect() {
40+
41+
if(stompClient != null) {
42+
stompClient.disconnect();
43+
}
44+
45+
setConnected(false);
46+
console.log("Disconnected");
47+
}
48+
49+
function sendMessage() {
50+
51+
var text = document.getElementById('text').value;
52+
stompClient.send("/app/chat", {}, JSON.stringify({ 'text': text }));
53+
}
54+
55+
function showMessageOutput(messageOutput) {
56+
57+
var response = document.getElementById('response');
58+
var p = document.createElement('p');
59+
p.style.wordWrap = 'break-word';
60+
p.appendChild(document.createTextNode(messageOutput.time + ": " + messageOutput.text));
61+
response.appendChild(p);
62+
}
63+
64+
</script>
65+
66+
</head>
67+
68+
<body onload="disconnect()">
69+
70+
<div>
71+
<div>
72+
<button id="connect" onclick="connect();">Connect</button>
73+
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
74+
</div>
75+
<br />
76+
<div id="conversationDiv">
77+
<input type="text" id="text" placeholder="Write a message..."/>
78+
<button id="sendMessage" onclick="sendMessage();">Send</button>
79+
<p id="response"></p>
80+
</div>
81+
</div>
82+
83+
</body>
84+
</html>

0 commit comments

Comments
 (0)