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);
    }
}
相关推荐
TeleostNaCl3 分钟前
Android TV | 一种不跳出应用指定页面的类 Monkey 的 Android TV 压测脚本
android·经验分享·压力测试
lihui_cbdd5 分钟前
Linux 压缩与解压终极指南:全格式、分卷与进阶操作
linux·运维·服务器
k***08297 分钟前
mysql中general_log日志详解
android·数据库·mysql
在天愿作比翼鸟在地愿为连理枝8 分钟前
UTM+Ubuntu配置MPICH并行环境
linux·运维·ubuntu
r***869818 分钟前
Nginx解决前端跨域问题
运维·前端·nginx
2501_9371549319 分钟前
酷秒神马 9.0 版源码系统实测
android·源码·源代码管理·机顶盒
落日漫游24 分钟前
ansible中角色概念
运维·云原生·自动化
2301_8072886328 分钟前
MPRPC项目制作(第四天)
java·服务器·前端
LCG米34 分钟前
工业自动化嵌入式开发实战:基于ARM7与μC/OS-II的焊接机控制系统设计与实现
运维·c语言·自动化
@YDWLCloud42 分钟前
做独立站,用阿里云国际版还是 Cloudflare?答案出乎意料
服务器·网络·阿里云·云计算