first successful ws impl
This commit is contained in:
parent
74700bfdc2
commit
4329c5067f
14 changed files with 175 additions and 154 deletions
60
src/main/resources/static/app.js
Normal file
60
src/main/resources/static/app.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
const stompClient = new StompJs.Client({
|
||||
brokerURL: 'ws://localhost:8080/ws'
|
||||
});
|
||||
|
||||
stompClient.onConnect = (frame) => {
|
||||
setConnected(true);
|
||||
console.log('Connected: ' + frame);
|
||||
stompClient.subscribe('/sub/chat', (greeting) => {
|
||||
showGreeting(JSON.parse(greeting.body).content);
|
||||
});
|
||||
};
|
||||
|
||||
stompClient.onWebSocketError = (error) => {
|
||||
console.error('Error with websocket', error);
|
||||
};
|
||||
|
||||
stompClient.onStompError = (frame) => {
|
||||
console.error('Broker reported error: ' + frame.headers['message']);
|
||||
console.error('Additional details: ' + frame.body);
|
||||
};
|
||||
|
||||
function setConnected(connected) {
|
||||
$("#connect").prop("disabled", connected);
|
||||
$("#disconnect").prop("disabled", !connected);
|
||||
if (connected) {
|
||||
$("#conversation").show();
|
||||
}
|
||||
else {
|
||||
$("#conversation").hide();
|
||||
}
|
||||
$("#greetings").html("");
|
||||
}
|
||||
|
||||
function connect() {
|
||||
stompClient.activate();
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
stompClient.deactivate();
|
||||
setConnected(false);
|
||||
console.log("Disconnected");
|
||||
}
|
||||
|
||||
function sendName() {
|
||||
stompClient.publish({
|
||||
destination: "/app/chat",
|
||||
body: JSON.stringify({'text': $("#name").val()})
|
||||
});
|
||||
}
|
||||
|
||||
function showGreeting(message) {
|
||||
$("#greetings").append("<tr><td>" + message + "</td></tr>");
|
||||
}
|
||||
|
||||
$(function () {
|
||||
$("form").on('submit', (e) => e.preventDefault());
|
||||
$( "#connect" ).click(() => connect());
|
||||
$( "#disconnect" ).click(() => disconnect());
|
||||
$( "#send" ).click(() => sendName());
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue