78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
![]() |
const stompClient = new StompJs.Client({
|
||
|
brokerURL: 'ws://localhost:9999/admin/rax/doctor-medicine'
|
||
|
});
|
||
|
/*const testClient = new StompJs.Client({
|
||
|
brokerURL: "ws://localhost:8080/test-guide"
|
||
|
})*/
|
||
|
|
||
|
stompClient.onConnect = (frame) => {
|
||
|
setConnected(true);
|
||
|
console.log('Connected: ' + frame);
|
||
|
stompClient.subscribe('/topic/doctorMedicine', (greeting) => {
|
||
|
showGreeting(greeting.body);
|
||
|
});
|
||
|
};
|
||
|
|
||
|
/*testClient.onConnect = (data) => {
|
||
|
console.log('Connected: ' + data);
|
||
|
testClient.subscribe('/topic/test', (greeting) => {
|
||
|
console.log(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();
|
||
|
// testClient.activate();
|
||
|
}
|
||
|
|
||
|
function disconnect() {
|
||
|
stompClient.deactivate();
|
||
|
// testClient.deactivate();
|
||
|
setConnected(false);
|
||
|
console.log("Disconnected");
|
||
|
}
|
||
|
|
||
|
function sendName() {
|
||
|
stompClient.publish({
|
||
|
destination: "/front/doctorMedicine",
|
||
|
body: $("#name").val()
|
||
|
});
|
||
|
/*testClient.publish({
|
||
|
destination: "/app/test-hello",
|
||
|
body: JSON.stringify({'name': "test-hello"})
|
||
|
})*/
|
||
|
}
|
||
|
|
||
|
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());
|
||
|
});
|
||
|
|