on(‘close’, function() {…})

on(‘error’, function(err) {…})

on(‘return’, function(msg) {…})

on(‘drain’, function() {…})

  • close,channel关闭
  • error,channel异常
  • return,???
  • drain,???
    1. (async () => {
    2. try {
    3. const connection = await amqp.connect(url)
    4. const channel = await connection.createChannel();
    5. const queue = 'hello';
    6. const msg = 'Hello World!';
    7. await channel.assertQueue(queue, { durable: false });
    8. await channel.sendToQueue(queue, Buffer.from(msg));
    9. console.log(" [x] Sent %s", msg);
    10. channel.on('close', () => {
    11. console.log('channel.on close')
    12. })
    13. channel.on('error', (err) => {
    14. console.log('channel.on error:', err)
    15. })
    16. channel.on('return', (msg) => {
    17. console.log('channel.on return:', msg)
    18. })
    19. channel.on('drain', () => {
    20. console.log('channel.on drain')
    21. })
    22. await channel.close();
    23. setTimeout(async () => await connection.close(), 500)
    24. } catch (e) {
    25. console.log(e)
    26. }
    27. })()