Java-2602 Check spring-websockets PR (#10002)

Co-authored-by: mikr <michael.krimgen@ximedes.com>
This commit is contained in:
Maiklins 2020-09-10 19:26:04 +02:00 committed by GitHub
parent 0acc6a25e1
commit fdc36375ff
1 changed files with 20 additions and 20 deletions

View File

@ -2,63 +2,63 @@
<head> <head>
<title>Chat WebSocket</title> <title>Chat WebSocket</title>
<script src="./js/sockjs-0.3.4.js"></script> <script src="resources/js/sockjs-0.3.4.js"></script>
<script src="./js/stomp.js"></script> <script src="resources/js/stomp.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var stompClient = null; var stompClient = null;
function setConnected(connected) { function setConnected(connected) {
document.getElementById('connect').disabled = connected; document.getElementById('connect').disabled = connected;
document.getElementById('disconnect').disabled = !connected; document.getElementById('disconnect').disabled = !connected;
document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden'; document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
document.getElementById('response').innerHTML = ''; document.getElementById('response').innerHTML = '';
} }
function connect() { function connect() {
var socket = new SockJS('/spring-mvc-java/chat'); var socket = new SockJS('/chat');
stompClient = Stomp.over(socket); stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) { stompClient.connect({}, function(frame) {
setConnected(true); setConnected(true);
console.log('Connected: ' + frame); console.log('Connected: ' + frame);
stompClient.subscribe('/topic/messages', function(messageOutput) { stompClient.subscribe('/topic/messages', function(messageOutput) {
showMessageOutput(JSON.parse(messageOutput.body)); showMessageOutput(JSON.parse(messageOutput.body));
}); });
}); });
} }
function disconnect() { function disconnect() {
if(stompClient != null) { if(stompClient != null) {
stompClient.disconnect(); stompClient.disconnect();
} }
setConnected(false); setConnected(false);
console.log("Disconnected"); console.log("Disconnected");
} }
function sendMessage() { function sendMessage() {
var from = document.getElementById('from').value; var from = document.getElementById('from').value;
var text = document.getElementById('text').value; var text = document.getElementById('text').value;
stompClient.send("/app/chat", {}, JSON.stringify({'from':from, 'text':text})); stompClient.send("/app/chat", {}, JSON.stringify({'from':from, 'text':text}));
} }
function showMessageOutput(messageOutput) { function showMessageOutput(messageOutput) {
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.from + ": " + messageOutput.text + " (" + messageOutput.time + ")")); p.appendChild(document.createTextNode(messageOutput.from + ": " + messageOutput.text + " (" + messageOutput.time + ")"));
response.appendChild(p); response.appendChild(p);
} }
</script> </script>
</head> </head>