php mosquitto,php使用Mosquitto扩展

订阅a.php

use Mosquitto\Client;

class MqttServer

{

private $mqtt;

public function __construct()

{

$this->mqtt = new Client();

$base = str_replace('a.php', '', __FILE__);

$this->mqtt->setTlsCertificates($base . 'key/ca.crt', $base . 'key/client.crt', $base . 'key/client.key');

$this->mqtt->setTlsOptions(Client::SSL_VERIFY_NONE, null, null);

$this->mqtt->setCredentials('root', '123456');

$this->mqtt->connect("127.0.0.1", 8883, 5);

$this->mqtt->onConnect([$this, 'handConnect']);

$this->mqtt->onMessage([$this, 'handleMessage']);

$this->mqtt->onDisconnect([$this, 'handleDisconnect']);

}

public function subscribeToTopics($topics)

{

foreach ($topics as $topic => $qos) {

$this->mqtt->subscribe($topic, $qos);

}

}

public function handConnect($rc)

{

echo "连接成功$rc\n";

}

public function publishData($msg,$topic,$qos)

{

$this->mqtt->publish($topic, $msg, $qos,false);

}

public function handleMessage($message)

{

printf("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);

}

public function handleDisconnect($rc)

{

echo "连接断开$rc\n";

}

public function start()

{

try {

$this->mqtt->loop();

} catch (\Throwable $e) {

$this->mqtt->connect("127.0.0.1", 8883, 5);

}

}

}

$logger = new MqttServer();

$logger->subscribeToTopics(['test'=>0]);

while (true) {

$logger->start();

}

发布b.php

use Mosquitto\Client;

class MqttServer

{

private $mqtt;

public function __construct()

{

$this->mqtt = new Client();

$base = str_replace('b.php', '', __FILE__);

$this->mqtt->setTlsCertificates($base . 'key/ca.crt', $base . 'key/client.crt', $base . 'key/client.key');

$this->mqtt->setTlsOptions(Client::SSL_VERIFY_NONE, null, null);

$this->mqtt->setCredentials('root', '123456');

$this->mqtt->connect("127.0.0.1", 8883, 5);

$this->mqtt->onConnect([$this, 'handConnect']);

$this->mqtt->onMessage([$this, 'handleMessage']);

$this->mqtt->onDisconnect([$this, 'handleDisconnect']);

}

public function subscribeToTopics($topics)

{

foreach ($topics as $topic => $qos) {

$this->mqtt->subscribe($topic, $qos);

}

}

public function handConnect($rc)

{

echo "连接成功$rc\n";

}

public function publishData($msg, $topic, $qos)

{

$mgid=$this->mqtt->publish($topic, $msg, $qos, false);

if ($mgid) echo '推送成功'.$mgid."\r\n";

}

public function handleMessage($message)

{

printf("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);

}

public function handleDisconnect($rc)

{

echo "连接断开$rc\n";

}

public function start()

{

try {

$this->mqtt->loop();

} catch (\Throwable $e) {

$this->mqtt->connect("127.0.0.1", 8883, 5);

}

}

}

$logger = new MqttServer();

while (true) {

sleep(2);

$msg = time() . '消息推送';

$logger->publishData($msg, 'test', 0);

$logger->start();

}

分别运行订阅与发布文件

f061bb1122cf

image.png

f061bb1122cf

image.png

php操作Mosquitto相关文档

https://mosquitto-php.readthedocs.io/en/latest/client.html

https://www.hivemq.com/blog/mqtt-client-library-encyclopedia-mosquitto-php/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值