我已经找到了有关在RabbitMQ中发送字符串示例并从队列中接收的代码,但我对以下方法不是很清楚:assertQueue和sendToQueue。
send.js
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var q = 'hello';
var msg = 'Hello World! - '+i;
ch.assertQueue(q, {durable: false});
ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() { conn.close(); process.exit(0) }, 1000);
});
receive.js
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) { //amqp://localhost
conn.createChannel(function(err, ch) {
var q = 'hello';
ch.assertQueue(q, {durable: false});
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
ch.consume(q, function(msg) {
console.log(" [x] Received %s", msg.content.toString());
}, {noAck: true});
});
});