thinkphp+ rabbitmq发送16进制到队列

猿掌柜
2021-04-19 / 0 评论 / 37 阅读 / 正在检测是否收录...

首先安装模块

composer require php-amqplib/php-amqplib

然后代码如下

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;


$chanflag='04';
$b1='00';
$b2='00';
$arguments = new AMQPTable();
$arguments['x-max-length']=1;//此参数限制队列最大一条数据
$connection = new AMQPStreamConnection('192.168.1.51', 5672, 'admin', '123456');
$channel = $connection->channel();
$channel->queue_declare('parameters', false, false, false, false,false,$arguments);
$sendStr = "a5 5a ".$chanflag." 00 00 00 00 ".$b1." ".$b2." 01"; 
$s=null;
$c = str_split(str_replace(' ', '', $sendStr), 2);  // 将16进制数据转换成两个一组的数组
foreach ($c as $key => $value) {
   $s.= chr(hexdec($c[$key]));
}
$msg = new AMQPMessage($s);
$channel->basic_publish($msg, '', 'hello');
$channel->close();
$connection->close(); 

具体相关配置请看上一篇,传送门

3

评论

博主关闭了当前页面的评论