如何读取stomp发送的消息体

时间:2017-08-16 15:47:34

标签: php activemq stomp

我试图创建一个我希望从一个制作人发送到另一个制作人的json,后来需要将其分解并使用它。 所以我使用activemq,并且stomp,因为我在php编程。 我的制作人有以下程序:

<?php

  namespace Stomp;

  require __DIR__ . '/../vendor/autoload.php';

  use Stomp\Client;
  use Stomp\StatefulStomp;
  use Stomp\Network\Connection;
  use Stomp\Transport\Message;

  header('Content-Type: application/json');
  //namespace buttoncall\Model;


  $provider=''; //colocar provider


  $data1= array(
        'provider' => $provider 
        );
(...)

$data=array($data1,$data2, $data3, $data4);
  $json = json_encode($data, true);

   $destination  = '/queue/nexmo';
  $messages = 1;
  $size = 1024;

  $DATA = "calls";
  //$body = $data;
  $body = $json;
  for($i=0; $i< $size; $i++) {
    $body .= $DATA[ $i % 26];
  } 

  try {
   $connection = new Connection('tcp://192.168.64.2:61613');
    $con1 = new StatefulStomp(new Client($connection));


      $con1->send($destination, new Message($body));

        echo "Message sent $body \n" ;

    $con1->send($destination, new Message("SHUTDOWN"));

  } catch(StompException $e) {
    echo $e->getMessage();

  }
}
}

然后是以下消费者:

<?php

//Nexmo

namespace Stomp;

require __DIR__ . '/../vendor/autoload.php';
include 'generate_jwt.php';

use Stomp\Client;
use Stomp\StatefulStomp;
use Stomp\Network\Connection;
use Stomp\Transport\Message;

$user = getenv("ACTIVEMQ_USER"); 
if( !$user ) $user = "admin";

$password = getenv("ACTIVEMQ_PASSWORD");
if( !$password ) $password = "admin";

/*$host = getenv("ACTIVEMQ_HOST");
if( !$host ) $host = "localhost";

$port = getenv("ACTIVEMQ_PORT");
if( !$port ) $port = 61613; */

$destination  = '/queue/nexmo';


try {
      $connection = new Connection('tcp://192.168.64.2:61613');
      $stomp = new StatefulStomp(new Client($connection));
      $stomp->subscribe($destination);


      echo "Waiting for messages...\n";
        while(true) {
                     $frame = $stomp->read();
                     echo "message received";
                     $json = $frame->$body;
                    //echo($stomp);
                    //echo($frame);
                    //echo($body);
                   echo $json ; 
                    }



} catch(StompException $e) {
  echo $e->getMessage();
} 

我已经尝试了几种组合来打印身体,但似乎没有任何效果......他们都是通信,我可以在ActiveMQ经纪人中看到它们,但我无法提取身体...... 。 有线索吗? 提前谢谢

1 个答案:

答案 0 :(得分:0)

我明白了,只是:

                 $frame = $stomp->read();
                 $body = $frame->getBody();
                 echo "message received $body \n";

在Stomp库中有一个函数。