1. 程式人生 > >node js -- 使用 amqplib模組

node js -- 使用 amqplib模組

var rabbitmq= {
        hostname:"192.1",
	port:"5672",
	username:"dep",
	password:"devl8",
	authMechanism: "AMQPLAIN" ,
	pathname:"/",
	ssl: { 
		enabled : false  
    }  
}


var open = require('amqplib').connect(rabbitmq).then(function(conn){
		  conn.createChannel().then((ch)=>{       //建立通道
			
			ch.assertExchange('api', 'topic',{durable:true}).then(function(ex) {    //連線交換機
					
					ch.assertQueue("topic").then(function(q) {     //連線佇列
                                                             //佇列     交換機     Rounting KEY   arguments
						 ch.bindQueue(q.queue,ex.exchange,"topic",{'x-match': 'any',
						 'foo': 'bar',
						 'baz': 'boo'})      //交換機bind佇列
						                               //Routing key
						var a =ch.publish(ex.exchange, 'topic', new Buffer(JSON.stringify({name:"chendong"})), {headers: {baz: 'boo'}})
					     if(a){
							console.log("傳送成功")
						//  conn.close()
						 }else{
							console.log("傳送失敗");
						//  conn.close()
						 }

						
					});
				});
		  })




},console.error)