94 lines
2.4 KiB
JavaScript
94 lines
2.4 KiB
JavaScript
const stompClient = new StompJs.Client({
|
|
brokerURL: 'ws://localhost:9999/admin/rax/SurgeryData'
|
|
});
|
|
/*const testClient = new StompJs.Client({
|
|
brokerURL: "ws://localhost:8080/test-guide"
|
|
})*/
|
|
|
|
let user = ""
|
|
|
|
stompClient.onConnect = (frame) => {
|
|
setConnected(true);
|
|
console.log('Connected: ' + frame);
|
|
/*stompClient.subscribe('/topic/user/'+user+'/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() {
|
|
sendNameToServer('stop');
|
|
stompClient.deactivate();
|
|
// testClient.deactivate();
|
|
setConnected(false);
|
|
console.log("Disconnected");
|
|
}
|
|
|
|
function sendName() {
|
|
user = Math.random()
|
|
$("#user-id").val(user)
|
|
sendNameToServer();
|
|
}
|
|
|
|
function sendNameToServer(status) {
|
|
/*stompClient.publish({
|
|
destination: "/front/doctorMedicine",
|
|
body: status ? status : $("#name").val()
|
|
});*/
|
|
stompClient.publish({
|
|
destination: "/front/doctorMedicine",
|
|
body: JSON.stringify({'status': status, "db": "test", user})
|
|
});
|
|
stompClient.subscribe('/topic/user/'+user+'/doctorMedicine', (greeting) => {
|
|
showGreeting(greeting.body);
|
|
});
|
|
/*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());
|
|
});
|
|
|