app.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var stompClient = null;
  2. function setConnected(connected) {
  3. $("#connect").prop("disabled", connected);
  4. $("#disconnect").prop("disabled", !connected);
  5. if (connected) {
  6. $("#conversation").show();
  7. }
  8. else {
  9. $("#conversation").hide();
  10. }
  11. $("#greetings").html("");
  12. }
  13. function connect() {
  14. var socket = new SockJS('http://localhost:8080/websocket');
  15. stompClient = Stomp.over(socket);
  16. stompClient.connect({}, function (frame) {
  17. setConnected(true);
  18. console.log('Connected: ' + frame);
  19. stompClient.subscribe('/topic/greetings', function (greeting) {
  20. showGreeting(JSON.parse(greeting.body).content);
  21. });
  22. });
  23. }
  24. function disconnect() {
  25. if (stompClient !== null) {
  26. stompClient.disconnect();
  27. }
  28. setConnected(false);
  29. console.log("Disconnected");
  30. }
  31. function sendName() {
  32. stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
  33. }
  34. function showGreeting(message) {
  35. $("#greetings").append("<tr><td>" + message + "</td></tr>");
  36. }
  37. $(function () {
  38. $("form").on('submit', function (e) {
  39. e.preventDefault();
  40. });
  41. $( "#connect" ).click(function() { connect(); });
  42. $( "#disconnect" ).click(function() { disconnect(); });
  43. $( "#send" ).click(function() { sendName(); });
  44. });