如何在Node.js中重新连接后正确检查RabbitMQ通道是否已打开?

如何在Node.js中重新连接后正确检查RabbitMQ通道是否已打开?,node.js,rabbitmq,amqp,Node.js,Rabbitmq,Amqp,我正在使用amqplib,我试图实现一种重新连接机制。然而,在重新建立连接后,我的频道似乎仍然关闭。如何解决此问题?这是我的密码 var pubQueue = []; module.exports = { connect: function (callback) { var self = this; amqp.connect(config.queue.url, function(err, connection) { if (err) { conso

我正在使用
amqplib
,我试图实现一种重新连接机制。然而,在重新建立连接后,我的频道似乎仍然关闭。如何解决此问题?这是我的密码

var pubQueue = [];
module.exports = {
  connect: function (callback) {
    var self = this;
    amqp.connect(config.queue.url, function(err, connection) {
      if (err) {
        console.error("[AMQP]", err.message);
        return setTimeout(module.exports.connect, 2000);
      }

      connection.on("error", function(err) {
        if (err.message !== "Connection closing") {
          console.error("[AMQP] conn error", err.message);
        }
      });

      connection.on("close", function() {
        console.error("[AMQP] reconnecting");
        return setTimeout(module.exports.connect, 2000);
      });

      connection.createChannel(function(err, ch) {
        console.log('connection is reestablished');
        self.channel = ch;
        return callback ? callback() : false;
      });
    });
  },

  publish: function (message, callback) {
    var self = this;
    var key = this.generateKey(message);
    var m = new Buffer(JSON.stringify(message));

    try {
      self.channel.assertExchange(config.queue.exchange, 'topic', {durable: false, nowait: true});
      self.channel.publish(config.queue.exchange, key, m, {nowait: true});
      return callback ? callback() : true;
    } catch(err) {
      pubQueue.push({key: key, m: m});
      console.log(err);
    }
  }
} 
express应用程序启动后将调用
connect()
。但是每次请求都会调用
publish
。这就是为什么我有
pubQueue
来存储丢失的消息。我还没有实现在队列中重新发送消息的功能,但我首先遇到了这个错误,我似乎无法理解它

connection is reestablished
{ [IllegalOperationError: Channel closed]
  message: 'Channel closed',
  stack: 'IllegalOperationError: Channel closed\n    at Channel.}

fwiw,有一个名为wascaly的库,构建在amqplib之上,其中内置了以下功能: