Ai
1 Star 0 Fork 1

谷动谷力/MQTT-Client-Examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
simps_pubsub.php 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
wivwiv 提交于 2022-04-25 11:43 +08:00 . feat(php): Add simps sample
<?php
require('vendor/autoload.php');
use Simps\MQTT\Client;
use Simps\MQTT\Config\ClientConfig;
use Swoole\Coroutine;
use Simps\MQTT\Protocol\Types;
function getConfig()
{
$config = new ClientConfig();
$config->setUserName('emqx_user')
->setPassword('public')
->setClientId('emqx_example_'.Client::genClientID())
->setKeepAlive(10)
// 3,4,5
->setProtocolLevel(5);
return $config;
}
// Subscribe
// https://github.com/simps/mqtt/blob/master/examples/v5/subscribe.php
Coroutine\run(function() {
$config = getConfig();
$client = new Client('broker.emqx.io', 1883, $config);
$client->connect();
$client->subscribe(['emqx/test' => 0]);
while(true) {
$buffer = $client->recv();
var_dump($buffer);
};
});
// Publish
// https://github.com/simps/mqtt/blob/master/examples/v5/publish.php
Coroutine\run(function () {
// refer: https://github.com/simps/mqtt/blob/master/examples/bootstrap.php
$config = getConfig();
$client = new Client('broker.emqx.io', 1883, $config);
$client->connect();
$payload = array(
'protocol' => 'MQTT 5.0',
'date' => date('Y-m-d H:i:s'),
'url' => 'https://github.com/emqx/MQTT-Client-Examples');
while (true) {
$response = $client->publish(
'emqx/test',
json_encode(
$payload
),
1,
0,
0,
[
'topic_alias' => 1,
'message_expiry_interval' => 12,
]
);
$msgid = $response['message_id'];
printf("msg $msgid send\n");
Coroutine::sleep(3);
}
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lojam/MQTT-Client-Examples.git
git@gitee.com:lojam/MQTT-Client-Examples.git
lojam
MQTT-Client-Examples
MQTT-Client-Examples
master

搜索帮助