Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Connection parameters
- var client; //Variable client must be declared outside connect()
- var mq_username = "guest",
- mq_password = "guest",
- mq_vhost = "/",
- mq_url = 'http://localhost:15674/stomp',
- // The queue we will read. The /topic/ queues are temporary
- // queues that will be created when the client connects, an
- // removed when the client disconnects. They will receive
- // all messages published in the "amq.topic" exchange, with the
- // given routing key, in this case "mymessages"
- mq_queue = "/queue/test",
- subscribe_headers = {ack: 'client'}; //This header is required to ensure client must acknowledge to every message received
- //moved to function()
- /* var ws = new SockJS(mq_url);
- var client = Stomp.over(ws);
- client.heartbeat.outgoing = 0;
- client.heartbeat.incoming = 0;
- client.debug = function(str) {
- $("#debug").append(str + "<br>");
- };*/
- // This will be called upon successful connection
- function on_connect() {
- $('#output').append('Connected to STOMP' + "<br>");
- console.log(client);
- id = client.subscribe(mq_queue, on_message, subscribe_headers);
- $('#output').append('Subscribed to ' + mq_queue + ' ID:' + id + "<br>");
- }
- // This will be called upon a connection error
- function on_connect_error(error) {
- $('#output').append('Connection failed! Error:'+ error + "<br>");
- $('#output').append('Disconnecting....' + "<br>");
- client.disconnect(function() {
- $('#output').append('Disconnected!' + "<br>");
- });
- $('#output').append('Reconnecting...' + "<br>");
- connect();
- }
- // This will be called upon arrival of a message
- function on_message(m) {
- console.log('message received');
- console.log(m);
- $('#output').append('[message-id:' + m.headers['message-id'] + '] --> ' + m.body + "<br>");
- client.ack(m.headers['message-id'], id); //This is to acknowledge the message received.
- }
- function connect(){
- console.log('Connecting...');
- // Connect
- var ws = new SockJS(mq_url);
- client = Stomp.over(ws);
- client.heartbeat.outgoing = 0;
- client.heartbeat.incoming = 0;
- client.debug = function (str) {
- $("#debug").append(str + "<br>");
- };
- client.connect(
- mq_username,
- mq_password,
- on_connect,
- on_connect_error,
- mq_vhost
- );
- }
- connect();
Advertisement
Add Comment
Please, Sign In to add comment