0

我正在使用 amqplib 创建 amqp 客户端。它在本地主机上运行时工作正常,但是当我将其更改为服务器的 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)
4

1 回答 1

1

我通过在 rabbitmq 代理上创建一个新用户来修复它。来宾用户仅适用于 localhost。有关更多详细信息,请参阅https://stackoverflow.com/a/26820152/7236105

于 2017-11-26T15:00:09.203 回答