2015-10-19 50 views
2

我正在編寫一個web應用程序來與基於RabbitMQ的軟件系統進行交互。我是網絡應用程序新手,但我已經使用MEAN堆棧編寫了相當數量的應用程序,我對此感到滿意。我發現這個鏈接:標題交換amqplib或其他js庫

amqp vs amqplib - which Node.js amqp client library is better?

這表明一些很好的替代庫,如bramqp和amqp.node,但我一直沒能找到答案以下問題。哪些庫(如果有的話)允許我與基於頭的交換進行交互?請注意,我很樂意將此作爲後續的原始鏈接發佈,但我無法弄清楚。

Howard

回答

0

我寫了一個教程來演示下面的標題交換。不久之後,會有像bramqp這樣的高級教程。這應該只打印其中一條消息。

var bramqp = require('bramqp'); 
var async = require('async'); 
var net = require('net'); 

var socket = net.connect({ 
    port : 5672 
}); 
bramqp.initialize(socket, 'rabbitmq/full/amqp0-9-1.stripped.extended', function(error, handle) { 
    async.series([ function(seriesCallback) { 
     handle.openAMQPCommunication('guest', 'guest', true, seriesCallback); 
    }, function(seriesCallback) { 
     handle.queue.declare(1, 'header_test_queue'); 
     handle.once('1:queue.declare-ok', function(channel, method, data) { 
      seriesCallback(); 
     }); 
    }, function(seriesCallback) { 
     handle.basic.consume(1, 'header_test_queue', null, false, true, false, false, {}); 
     handle.once('1:basic.consume-ok', function(channel, method, data) { 
      handle.on('1:basic.deliver', function(channel, method, data) { 
       handle.once('content', function(channel, className, properties, content) { 
        console.log('got a message:'); 
        console.log(content.toString()); 
       }); 
      }); 
      seriesCallback(); 
     }); 
    }, function(seriesCallback) { 
     handle.exchange.declare(1, 'header_test_exchange', 'headers', function() { 
      seriesCallback(); 
     }); 
    }, function(seriesCallback) { 
     exchange_arguments = { 
      'x-match': { 
       type: 'Long string', 
       data: 'all' 
      }, 
      wizard : { 
       type: 'Long string', 
       data: 'magic' 
      } 
     }; 
     handle.queue.bind(1, 'header_test_queue', 'header_test_exchange', null, false, exchange_arguments, function() { 
      seriesCallback(); 
     }); 
    }, function(seriesCallback) { 
     handle.basic.publish(1, 'header_test_exchange', null, false, false, function() { 
      properties = { 
       headers: { 
        wizard : { 
         type: 'Long string', 
         data: 'magic' 
        } 
       } 
      }; 
      handle.content(1, 'basic', properties, 'Hello World! with magic', seriesCallback); 
     }); 
    }, function(seriesCallback) { 
     handle.basic.publish(1, 'header_test_exchange', null, false, false, function() { 
      properties = { 
       headers: { 
        wizard : { 
         type: 'Long string', 
         data: 'not magic' 
        } 
       } 
      }; 
      handle.content(1, 'basic', properties, 'Hello World! without magic', seriesCallback); 
     }); 
    }, function(seriesCallback) { 
     setTimeout(function() { 
      handle.closeAMQPCommunication(seriesCallback); 
     }, 10 * 1000); 
    }, function(seriesCallback) { 
     handle.socket.end(); 
     setImmediate(seriesCallback); 
    } ], function() { 
     console.log('all done'); 
    }); 
});