Created
August 5, 2018 20:20
-
-
Save allandequeiroz/a9740820d2a346c9b5d5c6b2f070b79f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let stompClient = null; | |
function setConnected(connected) { | |
$("#connect").prop("disabled", connected); | |
$("#disconnect").prop("disabled", !connected); | |
if (connected) { | |
$("#conversation").show(); | |
} | |
else { | |
$("#conversation").hide(); | |
} | |
$("#greetings").html(""); | |
} | |
function connect() { | |
let socket = new SockJS('/email'); | |
stompClient = Stomp.over(socket); | |
stompClient.connect({}, function (frame) { | |
setConnected(true); | |
console.log('Connected: ' + frame); | |
stompClient.subscribe('/topic/email/updates', function (message) { | |
displayEmail(JSON.parse(message.body)); | |
}); | |
stompClient.send('/app/emails'); | |
}); | |
} | |
function disconnect() { | |
if (stompClient !== null) { | |
stompClient.disconnect(); | |
} | |
setConnected(false); | |
console.log("Disconnected"); | |
} | |
function sendEmail() { | |
const message = { | |
'content': $("#message").val() | |
}; | |
stompClient.send('/app/email', {}, JSON.stringify(message)); | |
$("#message").val(""); | |
} | |
function displayEmail(message) { | |
$("#emails").prepend( | |
"<tr id='" + message.id + "'><td>" + message.content + "</td></tr>" | |
); | |
} | |
$(function () { | |
$("form").on('submit', function (e) { | |
e.preventDefault(); | |
}); | |
connect(); | |
$("#send").click(function () { | |
sendEmail(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment