当前位置:首页 > 技术分享 > 正文内容

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

admin2年前 (2023-06-02)技术分享2284


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

分享给朋友:

相关文章

百度云,天翼云盘解析网页地址 收集分享

百度云解析http://p.106666.xyz/https://pan.kdbaidu.com/https://pan.kdpd.me/https://yun.kdbaidu.com/http://blog.xxatf.top/https...

php高效检测远程图片是否存在

php高效检测远程图片是否存在function img_exits($url){     $ch = curl_init();    &...

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

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

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

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

[Windows] Adobe Flash Player 34.0.0.92及可用版修改方法

[Windows] Adobe Flash Player 34.0.0.92及可用版修改方法

随着 2021 年的到来,Adobe Flash Player 也迎来了告别,Adobe 在 2020 年 12 月 31 日后将不再支持 Flash Player。其实早在 2017 年,Adobe 公司就已宣布,计划在 2020 年底逐...

超高性比的斐讯盒子T1,刷第三方YYF固件机教程超级详细版

超高性比的斐讯盒子T1,刷第三方YYF固件机教程超级详细版

家里面买了斐讯盒子T1,必不可少的就是刷机,刷机一直爽,一直刷机一直爽,这样的快乐一般人体会不到。原来斐讯盒子N1,T1,还有斐讯K2P路由器也变成了性价比超高的东东,而且众多大神也带来了超多可玩性非常高的固件和破解。楼主今天扒到了相关超高...

发表评论

访客

看不清,换一张

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