我正在使用amqplib来创建amqp客户端。它在localhost上运行时工作正常,但当我将其更改为服务器的IP地址192.168.1.44时,我收到一个错误,指示conn对象未定义。
这是客户的代码
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://guest:guest@192.168.1.44:5672', function(err, conn) {
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});
});
});
这是错误消息
/home/pi/Desktop/mqtt_example/receive.js:14
conn.createChannel(function(err, ch) {
^
TypeError: Cannot read property 'createChannel' of undefined
at /home/pi/Desktop/mqtt_example/receive.js:14:5
at /home/pi/Desktop/mqtt_example/node_modules/amqplib/callback_api.js:16:10
at /home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connect.js:164:12
at bail (/home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:176:5)
at afterStartOk (/home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:219:7)
at /home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:160:12
at Socket.recv (/home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:498:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:105:13)
at Socket.emit (events.js:207:7)
at emitReadable_ (_stream_readable.js:502:10)
at emitReadable (_stream_readable.js:496:7)
at addChunk (_stream_readable.js:263:7)
at readableAddChunk (_stream_readable.js:239:11)
at Socket.Readable.push (_stream_readable.js:197:10)
at TCP.onread (net.js:588:20)
答案 0 :(得分:1)
我通过在rabbitmq代理上创建一个新用户来修复它。 guest用户只能使用localhost。有关详细信息,请参阅https://stackoverflow.com/a/26820152/7236105。