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

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

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


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的实例” 的相关文章

input search更改默认删除按钮的样式

改input输入框search属性下输入文字之后会在输入框最后出现一个默认样式的X不过这个样式不太好看想自定义怎么办呢方法一input[type="search"]::-webkit-search-cancel-butt…

贾氏鸣天鼓健耳养肾操

贾氏鸣天鼓健耳养肾操

《贾氏鸣天鼓健耳养肾操》鸣天鼓是健耳强肾治耳病的古法,贾氏越云自创的鸣天鼓健耳养肾操是在古法的基础上创建。顺序:1静坐挺胸。2双手放心脏位置的胸口,左手掌盖住右手掌。3闭目静心,深呼吸19下。4双手相互搓揉,让手掌发热。5用发热的双手手掌严…

微软Windows 10升级密钥(例如家庭版升级为企业版) 不能用于激活系统

微软Windows 10升级密钥(例如家庭版升级为企业版) 不能用于激活系统

下面的密钥,是微软官方提供的,仅能用于Windows10系统版本的升级,比如从家庭版升级为专业版、专业版升级为企业版等。升级密钥不能用于激活系统,激活需要KMS或者数字权利,由于涉及到版权问题,在此不宜分享,请大家自行查找激活相关的内容。准…

PIP 更换国内安装源linux/windows

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

python3 selenium webdriver.Chrome php 爬取汽车之家所有车型详情数据[开源版]

介绍本接口是车型库api的补充,用于爬取汽车之家所有车型详情数据开源地址:https://gitee.com/web/CarApi/tree/master/python软件架构python3 selenium webdriver.Chrom…

Linux/centos inode 占用100%的解决办法

当你的 Linux 系统无法创建新文件时,有可能是你的磁盘满了,还有可能是你的磁盘的 inode 用光了,我们今天要说的就是后一种情况,要解决这个问题,只能是删除一些文件,但是一般情况下,其实是你的系统中的某个地方产生了大量的你并不需要的文…

发表评论

访客

看不清,换一张

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