参考 Documentation - PHP | CloudMQTTHosted message broker for the Internet of Things. Perfectly configured and optimized message queues for IoT, ready in seconds.https://www.cloudmqtt.com/docs-php.html
下载 GitHub - bluerhinos/phpMQTT: a simple php class to connect/publish/subscribe to a MQTT brokera simple php class to connect/publish/subscribe to a MQTT broker - GitHub - bluerhinos/phpMQTT: a simple php class to connect/publish/subscribe to a MQTT broker
文章图片
https://github.com/bluerhinos/phpMQTT
CloudMQTT 概述 Documentation | CloudMQTTHosted message broker for the Internet of Things. Perfectly configured and optimized message queues for IoT, ready in seconds.https://www.cloudmqtt.com/docs.html
客户端发布
/**
* Created by PhpStorm.
* Time: 16:47
*/
namespace App\Http\Controllers\Mqtt\v1;
use App\Common\Common;
use App\Http\Controllers\Controller;
use App\until\phpMQTT;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use App\Events\Handle;
class Subscript extends Controller
{
/*
手动执行监听 MQTT 订阅;
* php artisan Mqtt
*/
//所有配置放在 配置文件config中
public function subscript(){
$server = config('mqtt.host');
// change if necessary 服务器IP
$port = config('mqtt.port');
// change if necessary端口 一般是1883
$username = config('mqtt.username');
// set your username mosquitto设置的用户名
$password = config('mqtt.password');
// set your password mosquitto设置的密码
$client_id = config('mqtt.clientId');
//你的连接客户端id
$keepAlive = config('mqtt.keepAlive');
$mqtt = new phpMQTT($server, $port, $client_id,'',$keepAlive);
//进行连接
//cleanSession 设为false,客户端掉线后 服务器端不会清除session,当重连后可以接收之前订阅主题的消息。
//当客户端上线后会接受到它离线的这段时间的消息
//但是这个只是进行了重连,重连后还需要再次发起订阅
//接收遗嘱消息
if(!$mqtt->connect(true, NULL, $username, $password)) {
exit('error');
//连接失败
} else {
echo "success";
//连接成功
}
//topics["topic"]为接受的主题名需要和发送的主题名一致否则会订阅不到
//订阅信息 Qos为信息登记,需要和发送的等级一致
//可以订阅多个不同类型通知
$topics["SERVER/#"] = array("qos" => 0, "function" =>array($this,"onMessage"));
$topics["IM/#"] = array("qos" => 0, "function" =>array($this,"onImMessage"));
$mqtt->subscribe($topics, 0);
//死循环监听
while($mqtt->proc()){
}
$mqtt->close();
}
/**
* 在此处接MQtt的信息 进行业务处理
* @param $topic
* @param $msg
* 设备订阅主题
* device/xxxxx/硬件号
* 设备发布的主题
* server/xxxxx/硬件号
* 设备遗嘱主题
* will/xxxxx/硬件号
* 处理逻辑写到handle文件中
* 处理逻辑写到handle文件中
* 处理逻辑写到handle文件中
*/
function onMessage($topic,$msg){
echo $topic;
echo "Msg Recieved: ".$msg;
//所有逻辑全移到handle
HandleController::business($topic,$msg);
}
/*
* 消息处理
*/
function onImMessage($topic,$msg){
//echo $msg;
echo $topic;
echo "Msg Recieved: ".$msg;
//所有逻辑全移到Im
ImMessageController::business($topic,$msg);
}
}
消息发送
namespace App\Http\Controllers\Mqtt\v1;
use App\Http\Controllers\Controller;
use App\until\phpMQTT;
use Illuminate\Http\Request;
class Publish extends Controller
{
/**
* @return bool
* 发送信息第三个参数为Qos服务质量等级
* Qos0发送者只发送一次消息,不进行重试,Broker不会返回确认消息。在Qos0情况下,Broker可能没有接受到消息
* Qos1发送者最少发送一次消息,确保消息到达Broker,Broker需要返回确认消息PUBACK。在Qos1情况下,Broker可能接受到重复消息
* Qos2Qos2使用两阶段确认来保证消息的不丢失和不重复。在Qos2情况下,Broker肯定会收到消息,且只收到一次
*/
public static function push($topic, $message, $QOS = 0, $will = null)
{
if (!$topic || !$message) {
return false;
}
//默认主题都大写。
//$topic=strtoupper($topic);
$host = config('mqtt.host');
//主机
$port = config('mqtt.port');
//端口
$username = config('mqtt.username');
//如果没有则为空
$password = config('mqtt.password');
//如果没有则为空
//phpMQTT 有四个参数:主机,端口,客户端id,证书。官网这里的案例没写证书,请参考phpMQTT类
//没有证书的时候只能连接1883端口,不能连接8883端口。
$mqtt = new phpMQTT($host, $port, "SERVER" . rand());
//连接
if ($mqtt->connect(true, $will, $username, $password)) {
$mqtt->publish($topic, $message, $QOS);
$mqtt->close();
//关闭
} else {
echo "Fail or time out
";
}
HandleController::logMqtt($topic, json_decode($message, true));
}
public function publish(Request $request)
{
$topic = $request->input('theme');
//发送的主题
$message = $request->input('content');
//要发送的消息
$QOS = $request->input('qos', 0);
//要发送的消息服务质量等级
//print_r($topic);
die;
//$willTopic = input('willTopic');
//$willMessage = input('willMessage');
//$willQos = input('willQos',0);
//$willRetain = input('willRetain',false);
//1true
if (!$topic) {
return returnJson(50000, "主题不能为空");
}
if (!$message) {
return returnJson(50000, "消息不能为空");
}
$will = null;
//if($willMessage && $willTopic){
//$will['topic'] = $willTopic;
//$will['content'] = $willMessage;
//$will['qos'] = $willQos;
//$will['retain'] = $willRetain;
//}
self::push($topic, $message, $QOS, $will);
}
}
Publish::push($theme, json_encode($pushDate,JSON_UNESCAPED_UNICODE), Common::QOS());
【php|PHPMqtt详解】
推荐阅读
- Matlab完整代码|【Matlab语言去噪】IIR+FIR滤波器语音去噪【含GUI源码 1027期】
- c语言|栈和队列c语言实现详解
- C语言|初识C语言
- 学习教程|php搭建网站入口搭建细节
- 程序员|Python爬虫系列(爬取小说并写入txt文件)
- python|python中使用xlrd、xlwt操作excel表格详解
- python|100天精通Python——第41天(自动化操作Excel(xlrd和xlwt))
- PHP中的stdClass是什么(如何使用?)
- php中|和||或运算符的区别是什么()