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

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

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


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…

[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 年底逐…

centos 配置Let's Encrypt 泛域名https证书

centos 配置Let's Encrypt 泛域名https证书

前言2018年1月份Letsencrypt可以申请泛域名证书,这让我们部署多域名、多站点https省了很多功夫,终于可以不用维护多个域名的https证书。笔者以acme.sh为例,手把手教你配置https证书~本教程适用于centos 6.…

系统小技巧:微软版“Ghost” Windows FFU 系统安装还原

系统小技巧:微软版“Ghost” Windows FFU 系统安装还原

在日常的维护中,系统的备份和还原是大家经常需要操作的事情。虽然Windows 10已经提供很多的工具,如系统还原、WIM备份/还原,VHD备份等。不过这些工具大多是基于文件的备份/还原。我们以前经常的使用的Ghost则是基于扇区的备份/还原…

PHP AES加解密 (ECB模式/sha1prng算法/PKCS5Padding和PKCS7Padding补码) ECB 模式不需求设置 iv

php7+ 版本/**  * [AesSecurity aes加密,支持PHP7+]  * 算法模式:ECB  * 密钥长度:128  * 补…

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

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

发表评论

访客

看不清,换一张

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