Skip to content

Commit ed72ff4

Browse files
committed
Add websocket example
1 parent 889b266 commit ed72ff4

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎spring-mvc-java/src/main/java/org/baeldung/model/Message.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
public class Message {
44

5+
private String from;
56
private String text;
67

78
public String getText() {
89
return text;
910
}
11+
12+
public String getFrom() {
13+
return from;
14+
}
1015
}

‎spring-mvc-java/src/main/java/org/baeldung/model/OutputMessage.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
public class OutputMessage {
44

5+
private String from;
56
private String text;
67
private String time;
78

8-
public OutputMessage(final String text, final String time) {
9+
public OutputMessage(final String from, final String text, final String time) {
10+
11+
this.from = from;
912
this.text = text;
1013
this.time = time;
1114
}
@@ -17,4 +20,8 @@ public String getText() {
1720
public String getTime() {
1821
return time;
1922
}
23+
24+
public String getFrom() {
25+
return from;
26+
}
2027
}

‎spring-mvc-java/src/main/java/org/baeldung/web/controller/ChatController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ChatController {
1717
public OutputMessage send(final Message message) throws Exception {
1818

1919
final String time = new SimpleDateFormat("HH:mm").format(new Date());
20-
return new OutputMessage(message.getText(), time);
20+
return new OutputMessage(message.getFrom(), message.getText(), time);
2121
}
2222

2323
}

‎spring-mvc-java/src/main/webapp/resources/chat.html‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@
4545

4646
function sendMessage() {
4747

48+
var from = document.getElementById('from').value;
4849
var text = document.getElementById('text').value;
49-
stompClient.send("/app/chat", {}, JSON.stringify({ 'text': text }));
50+
stompClient.send("/app/chat", {}, JSON.stringify({'from':from, 'text':text}));
5051
}
5152

5253
function showMessageOutput(messageOutput) {
5354

5455
var response = document.getElementById('response');
5556
var p = document.createElement('p');
5657
p.style.wordWrap = 'break-word';
57-
p.appendChild(document.createTextNode(messageOutput.time + ": " + messageOutput.text));
58+
p.appendChild(document.createTextNode(messageOutput.from + ": " + messageOutput.text + " (" + messageOutput.time + ")"));
5859
response.appendChild(p);
5960
}
6061

@@ -65,6 +66,12 @@
6566
<body onload="disconnect()">
6667

6768
<div>
69+
70+
71+
<div>
72+
<input type="text" id="from" placeholder="Choose a nickname"/>
73+
</div>
74+
<br />
6875
<div>
6976
<button id="connect" onclick="connect();">Connect</button>
7077
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>

0 commit comments

Comments
 (0)