dongyaocn/swoole-mqtt-client
Composer 安装命令:
composer require dongyaocn/swoole-mqtt-client
包简介
php swoole mqtt client
关键字:
README 文档
README
PHP mqtt client
usage
Install
Install:
composer require dongyaocn/swoole-mqtt-client
If your composer not allowed dev-master,add this config
"minimum-stability": "dev"
into your composer.json.
Require
swoole 2.0.8+
Example
define your logger:
class Logger implements \mqttclient\src\swoole\MqttLogInterface {
public function log($type,$content,$params = []){
echo "$type : $content \r\n";
}
}
define your tmp store (use Redis/Memory/...)
class Store implements \mqttclient\src\swoole\TmpStorageInterface{
private $data = [];
public function set($message_type, $key, $sub_key, $data, $expire = 3600)
{
$this->data[$message_type][$key][$sub_key] = $data;
}
public function get($message_type, $key, $sub_key)
{
return $this->data[$message_type][$key][$sub_key];
}
public function delete($message_type, $key, $sub_key)
{
if (!isset($this->data[$message_type][$key][$sub_key])){
echo "storage not found:$message_type $key $sub_key";
}
unset($this->data[$message_type][$key][$sub_key]);
}
}
use MqttClient
$host = '127.0.0.1';
$port = 1883;
$r = new \mqttclient\src\swoole\MqttClient($host,$port,10017);
$r->setAuth('username','password');
$r->setKeepAlive(60);
$r->setLogger(new Logger());
$r->setStore(new Store());
$r->setTopics(
[
new \mqttclient\src\subscribe\Topic('test/slim',function($msg){
echo "I receive:".$msg."\r\n";}),
new \mqttclient\src\subscribe\Topic('test/slim3',function(\mqttclient\src\swoole\MqttClient $client,$msg){
echo "I receive:".$msg." for slim3 \r\n";
echo $client->getClientId();
})
]
);
//set trigger
$r->on(\mqttclient\src\consts\ClientTriggers::RECEIVE_SUBACK,function(\mqttclient\src\swoole\MqttClient $client){
$client->publish('slim/echo','GGXX',\mqttclient\src\consts\Qos::ONE_TIME);
});
$r->connect();
$r->publish('test/slim','test qos',2);
Extends
You can also use own client extends MqttClient.
Example:
class Client extends MqttClient
{
private $mysql_handler;
private $mongo_handler;
public function __construct($host,$port,$client_id,$mysql_conf,$mongo_conf)
{
$this->mysql_handler = new Mysqli($mysql_conf);
$this->mongo_handler = new \MongoClient('mongodb://'.$mongo_conf['username'].':'.$mongo_conf['password'].'@'.$mongo_conf['host'].':'.$mongo_conf['port'].'/'.$mongo_conf['db']);
parent::__construct($host,$port,$client_id);
}
/**
* override the produceContainer function and map your own class/data/closure to the injector,and they can be used in every publish receive handler
* for exp: $client->setTopics([new Topic('test/own',function($mongo,$msg){ $result = $mongo->selectCollection('log_platform','test')->find(['sid' => ['$gte' => intval($msg)]]); })]);
* @return Injector
*/
protected function produceContainer()
{
$container = new Injector();
$container->mapData(MqttClient::class,$this);
$container->mapData(Client::class,$this);
$container->mapData('mysqli',$this->mysql_handler);
$container->mapData('mongo',$this->mongo_handler);
return $container;
}
}
dongyaocn/swoole-mqtt-client 适用场景与选型建议
dongyaocn/swoole-mqtt-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 71 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mqtt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dongyaocn/swoole-mqtt-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dongyaocn/swoole-mqtt-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dongyaocn/swoole-mqtt-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple Laravel Library to connect/publish/subscribe to MQTT broker
Asynchronous MQTT client built on React
A simple Laravel Library to connect/publish/subscribe to MQTT broker
MQTT protocol implementation
PHP micro framework for MQTT, letting you quickly connect, publish, and subscribe to topics.
Symfony Messenger transport for the MQTT.
统计信息
- 总下载量: 71
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-08