当前位置:首页 > 技术分享

centos MQTT安装和php使用mosquitto的实例

admin3年前 (2023-06-02)技术分享2639


MQTT 是物联网的消息传送协议标准。

在 CentOS 7上常用的开源 MQTT 消息服务器就是 Mosquitto


有人用 PECL 来安装 Mosquitto 的 PHP 实现,反正我没有成功

pecl install Mosquitto-alpha


也可以用yum安装 

yum install mosquitto mosquitto-devel

如果安装不成功就用编译安装 

包下载地址:https://github.com/mgdm/Mosquitto-PHP

phpize
 ./configure && make && make install

php.ini  添加 extension=mosquitto.so

开启mosquitto服务,

service mosquitto start
查看:systemctl status mosquitto.service
启动:systemctl start mosquitto.service
重启:systemctl restart mosquitto.service
自启:systemctl enable mosquitto.service

测试,如下图所示

  1. # 当前终端运行订阅测试

  2. mosquitto_sub -t test1

  3. # 另起一个终端运行发布测试,输入下面命令在订阅终端窗口可以看到Hello

  4. mosquitto_pub -t test1 -m Hello

关闭匿名登录,客户端登录需要用用户名,为了数据安全,这步也是必须要做的


# 到mosquitoo配置目录


cd /etc/mosquitto


# 修改配置


vim ./mosquitto.conf


# 输入 '/' 表示搜索,输入 allow_anonymous 查找到配置项,修改为false


allow_anonymous false


     具体配置如下


allow_anonymous false

password_file /etc/mosquitto/pwfile.txt

创建用户root

    命令行输入

  1. mosquitto_passwd -c /etc/mosquitto/pwfile.txt root

  2. # 接下来连续输入两次密码即可

                        

6. 重新启动(如果设定密码,必须要重启服务,以下两个命令都可以啊)

service mosquitto start
systemctl restart mosquitto.service

7. 最终测试,可以看到Welcome即成功

  1. # 当前终端运行订阅测试

  2. mosquitto_sub -t test -u root -P 密码

  3. # 另起一个终端运行发布测试

  4. mosquitto_pub -u root -P 密码 -t test -m Welcome

重启php

mqtt.php


<?php
$client = new Mosquitto\Client();
$client->setCredentials('root','12345');
$client->connect("127.0.0.1", 1884, 5);
 
for($i = 0;$i<=100;$i++) {
    $client->loop();
    $mid = $client->publish('ss', "Hello from PHP at " . date('Y-m-d H:i:s'), 1, 0);
    echo "Sent message ID: {$mid}\n";
    $client->loop();
 
    sleep(2);
}

sub.php


<?php
$c = new Mosquitto\Client;
$c->setCredentials('root','12345');
$c->connect('127.0.0.1',1883,50);
 
$c->subscribe('ss', 1);
 
$c->onMessage(function($m) {
    var_dump($m);
});
$c->loopForever();


扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/522.html

分享给朋友:

“centos MQTT安装和php使用mosquitto的实例” 的相关文章

解决 SVN Skipped 'xxx' -- Node remains in conflict

更新命令:svn up提示代码:意思就是说 ,这个文件冲突了,你要解决下Updating '.': Skipped 'data/config.php' -- …

2021可用的百度网盘高速下载方法分享

2021可用的百度网盘高速下载方法分享

最新可用方法https://blog.bitefu.net/post/163.html方法很简单就是利用网盘直链下载助手【网盘直链下载助手】是一款免费开源获取网盘文件真实下载地址的油猴脚本,基于PCSAPI,支持Windows,Mac,Li…

PIP 更换国内安装源linux/windows

pip国内的一些镜像  阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/   豆瓣(…

在CentOS 5.x 6.x里使用yum源 换阿里云国内源换vault.centos.org源

阿里云CentOS 5 的系统,无法用yum来安装应用软件。  原因:CentOS 5 在2017-03-31日已经结束支持,不再提供维护更新,所以包括阿里云镜像站的文件可能都是过时或已经有部分文件缺失。 &n…

python调用WinRAR暴力获取压缩密码 用网址做解压密码

原理很简单:python通过调用WinRAR.exe暴力获取压缩密码,要求必须安装WinRAR或者有WinRAR.exe这个文件(单个文件就行)。个人实测zip和rar格式都能用。缺点:费时间,费cpu一、不生成密码本:4位全数字密码:im…

apicloud影视APP源码 无需后台

apicloud影视APP源码 无需后台

介绍集合vip影视接口到一个android app中 方便观看各平台影视资源及直播开源地址:https://gitee.com/web/vip_yingshi软件架构使用apicloud搭建影视APP源码,无后台,调用接口同步api解析网址…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。