thinkphp链接mqtt服务器,然后在订阅下发布消息

cmd打开项目根目录,安装插件,执行下面的命令

javascript 复制代码
composer require php-mqtt/client

执行完成之后会在vendor 目录下有php-mqtt 文件

然后在你的 extend文件下 新建mqtt文件 在文件中新建 Mqtt.php 下面是代码

php 复制代码
<?php
/*
 * @S: ========================================================
 * @Name: 控制器: 
 * @Author: Fu
 * @Date: 2022-03-25 14:20:58
 * @FilePath: /hezonyuyin/extend/mqtt/Mqtt.php
 * @E: ========================================================
 */
namespace mqtt;

use PhpMqtt\Client\MQTTClient;
class Mqtt
{
    private $server;
    private $port;
    private $clientId;
    private $username;
    private $password;
    private $clean_session;
    public function __construct($server = '', $port = '', $clientId = '', $username = '', $password = '', $clean_session = '')
    {
        $this->server =  '127.0.0.1';//这里是你的服务器地址
        $this->port =  1883;
        $this->clientId =  'php-'.uniqid();
        $this->username =  'emqx_user';
        $this->password =  NULL;
        $this->clean_session  =  FALSE;
    }

    /**
     * @S: -------------------------------
     * @Name: 方法: 连接MQTT
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2022-03-31 09:26:12
     * @E: -------------------------------
     */    
    public function mqtt()
    {
        $mqtt = new MqttClient($this->server, $this->port, $this->clientId);
        $mqtt->connect($this->username, $this->password);
        $mqtt->loop(true);
    }

    /**
     * @S: ------------------------------
     * @Name: 方法: 发布订阅
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2022-03-25 14:22:42
     * @E: -------------------------------
     */    
    public function publish($topic,$content)
    {
        $mqtt = new MqttClient($this->server, $this->port, $this->clientId);
        $mqtt->connect($this->username, $this->password);
        $mqtt->publish($topic,$content,0,true);
    }
    
}

在项目的控制器的方法里,实现发布消息,方法如下

php 复制代码
<?php
namespace app\facemqtt\controller;

use think\Controller;
//引入extend文件夹里的类
use mqtt\Mqtt;
class Index extends Controller
{
    public function indexpage(){
        $this->push('topic');
        return 123;
    }
    private function push($topic, $data = [])
    {
        $mqtt = new Mqtt();
        $content = json_encode([
            'type' => 123,
            'time' => time(),
            'msg'  => '你好!',
        ]);
        //发布订阅消息,$topic 是主题,$content是发布的消息
        //然后订阅的这个主题的程序,就会收到$content消息
        $mqtt->publish($topic, $content);
    }
}
相关推荐
码云数智-园园1 分钟前
Android 全方位性能优化合集
android·性能优化
2601_949499944 分钟前
硬件工程师实操:芯瑞科技两款 400G 高速互联方案参数、场景、选型全解析
大数据·运维·人工智能·科技·光模块
雾时之林19 分钟前
Linux--介绍及管理
linux·运维·服务器
fhqlongteng35 分钟前
TCP服务器断开客户端的方法
服务器·网络·tcp/ip
啦啦啦啦啦zzzz41 分钟前
5种IO模型
运维·服务器·网络·c++
hunterandroid1 小时前
[Android 从零到一] Compose 动画实战:从 AnimatedVisibility 到复杂转场与手势联动
android
FreeTinker1 小时前
企业存储服务器NAS的选型逻辑与补充路径
运维·服务器
acrel71 小时前
算力机房供电不间断,智能电力运维保驾护航
运维·安全·机房电力运维·机房配电·列头柜监测
Knight_AL1 小时前
Lombok @Builder 踩坑:build() 前后对象类型不一样
android·java·开发语言
大黄说说2 小时前
Android 本地存储深度对比:SharedPreferences、MMKV、Room 数据库怎么选?
android·数据库