2012-08-23 139 views
2

我最近兩天瘋了,我無法建立與持久交換和持久隊列的NodeJS客戶端連接。NodeJS AMQP客戶端無法連接

所以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(); 
    } 

在屏幕上它說,messege發送。

接下來的事情是客戶端的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交換= connection.exchange ( 'biznea_e_1');該行,但不能聲明隊列。

我只想發送消息從PHP到NodeJS deamon,這就是全部!

幫助:)

+0

我退出使用這種方法,並開始使用Redis而不是AMQP,它在幾分鐘內更容易安裝和運行。 – theZiki

回答

1

試試這個:在Node.js的代碼,你在你的PHP代碼做了同樣的參數聲明與EXACTLY的交流和隊列。例如耐用。這可能會解決您的問題。

乾杯!

+0

如何爲節點設置這些參數? – theZiki

+0

你看過你正在使用的函數的node-amqp文檔嗎?它可能都在那裏。查看有關connection.exchange,connection.queue等的文檔。 – Sachin

0

看起來您正試圖在node.js代碼中再次創建交換「biznea_e_1」。它已經由php代碼創建。只嘗試訂閱。