on(‘close’, function() {…})
on(‘error’, function(err) {…})
on(‘return’, function(msg) {…})
on(‘drain’, function() {…})
- close,channel关闭
- error,channel异常
- return,???
- drain,???
(async () => {try {const connection = await amqp.connect(url)const channel = await connection.createChannel();const queue = 'hello';const msg = 'Hello World!';await channel.assertQueue(queue, { durable: false });await channel.sendToQueue(queue, Buffer.from(msg));console.log(" [x] Sent %s", msg);channel.on('close', () => {console.log('channel.on close')})channel.on('error', (err) => {console.log('channel.on error:', err)})channel.on('return', (msg) => {console.log('channel.on return:', msg)})channel.on('drain', () => {console.log('channel.on drain')})await channel.close();setTimeout(async () => await connection.close(), 500)} catch (e) {console.log(e)}})()
