Guest User

listener-app.js

a guest
Jul 25th, 2013
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Connection parameters
  2. var client;  //Variable client must be declared outside connect()
  3. var mq_username = "guest",
  4.     mq_password = "guest",
  5.     mq_vhost    = "/",
  6.     mq_url      = 'http://localhost:15674/stomp',
  7.  
  8.     // The queue we will read. The /topic/ queues are temporary
  9.     // queues that will be created when the client connects, an
  10.     // removed when the client disconnects. They will receive
  11.     // all messages published in the "amq.topic" exchange, with the
  12.     // given routing key, in this case "mymessages"
  13.  
  14.     mq_queue    = "/queue/test",
  15.     subscribe_headers = {ack: 'client'};  //This header is required to ensure client must acknowledge to every message received
  16. //moved to function()
  17. /*      var ws = new SockJS(mq_url);
  18.       var client = Stomp.over(ws);
  19.       client.heartbeat.outgoing = 0;
  20.       client.heartbeat.incoming = 0;
  21.       client.debug = function(str) {
  22.             $("#debug").append(str + "<br>");
  23.         };*/
  24.  
  25. // This will be called upon successful connection
  26. function on_connect() {
  27.   $('#output').append('Connected to STOMP' + "<br>");
  28.   console.log(client);
  29.   id = client.subscribe(mq_queue, on_message, subscribe_headers);
  30.   $('#output').append('Subscribed to ' + mq_queue + '  ID:' + id + "<br>");
  31. }
  32.  
  33. // This will be called upon a connection error
  34. function on_connect_error(error) {
  35.   $('#output').append('Connection failed! Error:'+ error + "<br>");
  36.     $('#output').append('Disconnecting....'  + "<br>");
  37.  client.disconnect(function() {
  38.     $('#output').append('Disconnected!' + "<br>");
  39.   });
  40.   $('#output').append('Reconnecting...' + "<br>");
  41.   connect();
  42. }
  43.  
  44. // This will be called upon arrival of a message
  45. function on_message(m) {
  46.   console.log('message received');
  47.   console.log(m);
  48.    $('#output').append('[message-id:' + m.headers['message-id'] + '] --> ' + m.body + "<br>");
  49.   client.ack(m.headers['message-id'], id);  //This is to acknowledge the message received.
  50. }
  51.  
  52. function connect(){
  53.   console.log('Connecting...');
  54.   // Connect
  55.   var ws = new SockJS(mq_url);
  56.     client = Stomp.over(ws);
  57.     client.heartbeat.outgoing = 0;
  58.     client.heartbeat.incoming = 0;
  59.     client.debug = function (str) {
  60.         $("#debug").append(str + "<br>");
  61.     };
  62.   client.connect(
  63.     mq_username,
  64.     mq_password,
  65.     on_connect,
  66.     on_connect_error,
  67.     mq_vhost
  68.   );
  69. }
  70.  
  71. connect();
Advertisement
Add Comment
Please, Sign In to add comment