Add websocket example

This commit is contained in:
Ivan Paolillo 2016-05-05 10:37:56 +02:00
parent 889b266107
commit ed72ff42e8
4 changed files with 23 additions and 4 deletions

View File

@ -2,9 +2,14 @@ package org.baeldung.model;
public class Message { public class Message {
private String from;
private String text; private String text;
public String getText() { public String getText() {
return text; return text;
} }
public String getFrom() {
return from;
}
} }

View File

@ -2,10 +2,13 @@ package org.baeldung.model;
public class OutputMessage { public class OutputMessage {
private String from;
private String text; private String text;
private String time; 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.text = text;
this.time = time; this.time = time;
} }
@ -17,4 +20,8 @@ public class OutputMessage {
public String getTime() { public String getTime() {
return time; return time;
} }
public String getFrom() {
return from;
}
} }

View File

@ -17,7 +17,7 @@ public class ChatController {
public OutputMessage send(final Message message) throws Exception { public OutputMessage send(final Message message) throws Exception {
final String time = new SimpleDateFormat("HH:mm").format(new Date()); final String time = new SimpleDateFormat("HH:mm").format(new Date());
return new OutputMessage(message.getText(), time); return new OutputMessage(message.getFrom(), message.getText(), time);
} }
} }

View File

@ -45,8 +45,9 @@
function sendMessage() { function sendMessage() {
var from = document.getElementById('from').value;
var text = document.getElementById('text').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) { function showMessageOutput(messageOutput) {
@ -54,7 +55,7 @@
var response = document.getElementById('response'); var response = document.getElementById('response');
var p = document.createElement('p'); var p = document.createElement('p');
p.style.wordWrap = 'break-word'; 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); response.appendChild(p);
} }
@ -65,6 +66,12 @@
<body onload="disconnect()"> <body onload="disconnect()">
<div> <div>
<div>
<input type="text" id="from" placeholder="Choose a nickname"/>
</div>
<br />
<div> <div>
<button id="connect" onclick="connect();">Connect</button> <button id="connect" onclick="connect();">Connect</button>
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button> <button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>