2013-04-22 181 views
8

我正在发送rabbitmq-tutorials,ruby版本正常,但node.js版本无法发送消息。我不知道什么是错的。node-amqp无法发送消息给RabbitMQ

var amqp  = require('amqp'); 
var amqp_hacks = require('./amqp-hacks'); 

var connection = amqp.createConnection({host: 'localhost'}); 

connection.on('ready', function(){ 
    connection.publish('hello_node', 'Hello World!'); 
    console.log(" [x] Sent 'Hello World!'"); 

    amqp_hacks.safeEndConnection(connection); 
}); 

后我跑node send.js,乳宁过程node recv.js不能recv的任何东西。并且rabbitmqctl list_queues不显示hello_node队列。

回答

5

您需要指出队列然后发布。 这个版本应该工作:

var amqp  = require('amqp'); 
    var amqp_hacks = require('./amqp-hacks'); 

    var connection = amqp.createConnection({host: 'localhost'}); 

    connection.on('ready', function(){ 
      connection.queue('hello_node', {'durable': false}, function(q){ 
       connection.publish('hello_node', 'Hello World!'); 
       console.log(" [x] Sent 'Hello World!' to 'hello_node'"); 

       amqp_hacks.safeEndConnection(connection); 
      }); 
    }); 
+0

谢谢你,它的工作原理 – nfpyfzyf 2013-04-23 01:01:03