Add websocket example
This commit is contained in:
parent
889b266107
commit
ed72ff42e8
|
@ -2,9 +2,14 @@ package org.baeldung.model;
|
|||
|
||||
public class Message {
|
||||
|
||||
private String from;
|
||||
private String text;
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,13 @@ package org.baeldung.model;
|
|||
|
||||
public class OutputMessage {
|
||||
|
||||
private String from;
|
||||
private String text;
|
||||
private String time;
|
||||
|
||||
public OutputMessage(final String text, final String time) {
|
||||
public OutputMessage(final String from, final String text, final String time) {
|
||||
|
||||
this.from = from;
|
||||
this.text = text;
|
||||
this.time = time;
|
||||
}
|
||||
|
@ -17,4 +20,8 @@ public class OutputMessage {
|
|||
public String getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ public class ChatController {
|
|||
public OutputMessage send(final Message message) throws Exception {
|
||||
|
||||
final String time = new SimpleDateFormat("HH:mm").format(new Date());
|
||||
return new OutputMessage(message.getText(), time);
|
||||
return new OutputMessage(message.getFrom(), message.getText(), time);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,8 +45,9 @@
|
|||
|
||||
function sendMessage() {
|
||||
|
||||
var from = document.getElementById('from').value;
|
||||
var text = document.getElementById('text').value;
|
||||
stompClient.send("/app/chat", {}, JSON.stringify({ 'text': text }));
|
||||
stompClient.send("/app/chat", {}, JSON.stringify({'from':from, 'text':text}));
|
||||
}
|
||||
|
||||
function showMessageOutput(messageOutput) {
|
||||
|
@ -54,7 +55,7 @@
|
|||
var response = document.getElementById('response');
|
||||
var p = document.createElement('p');
|
||||
p.style.wordWrap = 'break-word';
|
||||
p.appendChild(document.createTextNode(messageOutput.time + ": " + messageOutput.text));
|
||||
p.appendChild(document.createTextNode(messageOutput.from + ": " + messageOutput.text + " (" + messageOutput.time + ")"));
|
||||
response.appendChild(p);
|
||||
}
|
||||
|
||||
|
@ -65,6 +66,12 @@
|
|||
<body onload="disconnect()">
|
||||
|
||||
<div>
|
||||
|
||||
|
||||
<div>
|
||||
<input type="text" id="from" placeholder="Choose a nickname"/>
|
||||
</div>
|
||||
<br />
|
||||
<div>
|
||||
<button id="connect" onclick="connect();">Connect</button>
|
||||
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
|
||||
|
|
Loading…
Reference in New Issue