我最近两天疯了,我无法通过持久交换和持久队列在NodeJS客户端上建立连接。
所以PHP代码创建并发送消息:
<?php
$connection = new AMQPConnection(array(
'host' => 'localhost',
'vhost' => 'bvh',
'port' => 5672,
'login' => 'bizneus',
'password' => 'lozinkus'
));
//$connection = new AMQPConnection();
$connection->connect();
if (!$connection->isConnected()) {
die('Not connected :(' . PHP_EOL);
}
// Open Channel
$channel = new AMQPChannel($connection);
// Declare exchange
$exchange = new AMQPExchange($channel);
$exchange->setName('biznea_e_1');
$exchange->setType('fanout');
$exchange->setFlags(AMQP_DURABLE);
$exchange->declare();
// Create Queue
$queue = new AMQPQueue($channel);
$queue->setName('notify');
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$message = $exchange->publish(json_encode($s), 'kljuc');
if (!$message) {
echo 'Message not sent', PHP_EOL;
} else {
echo 'Message sent!', PHP_EOL;
}
if ($connection->isConnected()) {
$connection->disconnect();
}
在屏幕上显示发送了消息。
下一步是NodeJS客户端,它应该获取消息,但它不能:
var amqp = require('amqp');
var conParam = {
host: 'localhost',
port: 5672,
login: 'bizneus',
password: 'lozinkus',
vhost: 'bvh'
}
var connection = amqp.createConnection(conParam);
connection.on('ready', function(){
var exchange = connection.exchange('biznea_e_1');
var queue = connection.queue('notify');
queue.bind('biznea_e_1', 'kljuc');
queue.subscribe( {ack:true}, function(message){
var dj = JSON.parse(message.data.toString());
console.log(JSON.stringify(dj));
queue.shift();
});
});
但我收到此错误
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: PRECONDITION_FAILED - cannot redeclare exchange 'biznea_e_1' in vhost 'bvh' with different type, durable, internal or autodelete value
at Exchange._onMethod (/home/zijad/node_modules/amqp/amqp.js:1824:15)
at Exchange.Channel._onChannelMethod (/home/zijad/node_modules/amqp/amqp.js:1365:14)
at Connection._onMethod (/home/zijad/node_modules/amqp/amqp.js:922:28)
at AMQPParser.self.addListener.parser.onMethod (/home/zijad/node_modules/amqp/amqp.js:797:12)
at AMQPParser._parseMethodFrame (/home/zijad/node_modules/amqp/amqp.js:442:10)
at frameEnd (/home/zijad/node_modules/amqp/amqp.js:187:16)
at frame (/home/zijad/node_modules/amqp/amqp.js:172:14)
at AMQPParser.header [as parse] (/home/zijad/node_modules/amqp/amqp.js:159:14)
at AMQPParser.execute (/home/zijad/node_modules/amqp/amqp.js:231:21)
at Connection.<anonymous> (/home/zijad/node_modules/amqp/amqp.js:837:12)
我试图删除var exchange = connection.exchange('biznea_e_1');该行,但它不能声明队列。
我只想将消息从PHP发送到NodeJS deamon,这就是全部!
帮助:)
答案 0 :(得分:1)
试试这个:在node.js代码中,用 EXACTLY 声明交换和队列与PHP代码中的相同参数。例如耐用。这可以解决您的问题。
干杯!
答案 1 :(得分:0)
看起来您正在尝试在node.js代码中再次创建交换'biznea_e_1'。它已经由php代码创建。尽量只订阅。