您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman异步消息队列组件-stomp
发布时间:2021-12-31 21:43:10编辑:雪饮阅读()
workerman/stomp
STOMP是一个通讯协议。它是支持大多数消息队列如RabbitMQ、Apollo等。
安装
composer require workerman/stomp
然后配置好apache-activemq后启动apache-activemq如:
[root@localhost workerman]# apache-activemq-5.16.3/bin/activemq start
INFO: Loading '/www/wwwroot/workerman/apache-activemq-5.16.3//bin/env'
INFO: Using java '/www/wwwroot/workerman/java-se-8u41-ri//bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/www/wwwroot/workerman/apache-activemq-5.16.3//data/activemq.pid' (pid '39244')
然后apache-activemq启动后默认是启动了多个服务,其中stomp就在其中,默认端口61613
那么具体是stomp订阅、取消订阅、发布实例如:
<?php
use Workerman\Worker;
use Workerman\Timer;
use Workerman\Stomp\Client;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker();
$count=0;
$subscribe_id=null;
$worker->onWorkerStart = function(){
$client = new Workerman\Stomp\Client('stomp://127.0.0.1:61613');
$client->onConnect = function(Client $client) {
// 订阅
$client->subscribe('/topic/foo', function(Client $client, $data) {
global $subscribe_id;
if(!is_null($subscribe_id)){
//取消订阅
$client->unsubscribe($subscribe_id);
}
var_export($data);
});
};
$client->onError = function ($e) {
echo $e;
};
Timer::add(1, function () use ($client) {
// 发布
$client->send('/topic/foo', 'Hello Workerman STOMP');
},[],false);
$client->connect();
};
Worker::runAll();
实例运行:
[root@localhost workerman]# php stomp.php start
Workerman[stomp.php] start in DEBUG mode
------------------------------------- WORKERMAN --------------------------------------
Workerman version:4.0.26 PHP version:7.3.31
-------------------------------------- WORKERS ---------------------------------------
proto user worker listen processes status
tcp root none none 1 [OK]
--------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
array (
'cmd' => 'MESSAGE',
'headers' =>
array (
'content-length' => '21',
'expires' => '0',
'destination' => '/topic/foo',
'subscription' => 'client-1',
'priority' => '4',
'message-id' => 'ID:localhost.localdomain-38182-1640915994286-3:1:-1:1:1',
'content-type' => 'text/plain',
'timestamp' => '1640915999076',
),
'body' => 'Hello Workerman STOMP',
)
关键字词:workerman,stomp,消息队列