summaryrefslogtreecommitdiff
path: root/backend/src/main/resources/static/test-ws.js
blob: 1c64349e973328223aafe569219d0236018cc5fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var stompClient = null;

function connect() {
  console.log('Connecting...');
  var socket = new SockJS('/seven-wonders-websocket');
  stompClient = Stomp.over(socket);
  stompClient.connect({}, function (frame) {
    console.log('Connected: ' + frame);
    subscribeTo('/user/queue/errors');
  });
}

function send(endpoint, payload) {
    stompClient.send(endpoint, {}, payload);
}

function subscribeTo(endpoint) {
  $("#test-feeds").prepend('<tr><td>' + endpoint + '</td><td>Subscribed</td></tr>');
  stompClient.subscribe(endpoint, function (data) {
    $("#test-feeds").prepend('<tr><td>' + endpoint + '</td><td>Received: <pre>' + data.body + '</pre></td></tr>');
  });
}

$(function () {
  $("form").on('submit', function (e) {
    e.preventDefault();
  });
  $("#send-btn").click(function () {
    var endpoint = $("#path-field").val();
    var payload = $("#payload-field").val();
    send(endpoint, payload);
  });
  $("#subscribe-btn").click(function () {
    var endpoint = $("#subscribe-path-field").val();
    subscribeTo(endpoint);
  });
});

// auto-connect
connect();
bgstack15