elemenx/laravel-mqtt

一个简单的Laravel库,用于连接/发布/订阅MQTT代理

v2.0.8 2021-05-07 12:34 UTC

README

一个简单的Laravel库,用于连接/发布/订阅MQTT代理

基于bluerhinos/phpMQTT

例如,请查看此repo

安装

composer require elemenx/laravel-mqtt

功能

  • 用户名和密码认证
  • 客户端证书认证
  • 端到端加密的证书保护
  • 启用调试模式以简化调试
  • 现在您也可以设置您选择的Client_id,如果您不想使用,只需简单地不使用或将其设置为null
  • 直接从配置文件设置QOS标志
  • 直接从配置文件设置Retain标志
  • 添加辅助函数以使开发更加容易

启用包(可选)

此包实现了Laravel自动发现功能。安装后,会自动为laravel >= 5.5添加包提供者和外观。

如果您使用的是laravel版本< 5.5,则此步骤是必需的

要显式声明提供者和/或别名,请将服务提供者添加到您的config/app.php

'providers' => [

    Elemenx\Mqtt\MqttServiceProvider::class,

];

然后,将别名添加到您的config/app.php

'aliases' => [

    'Mqtt' => \Elemenx\Mqtt\Facades\Mqtt::class,

];

配置

发布配置文件

php artisan vendor:publish --provider="Elemenx\Mqtt\MqttServiceProvider"

Config/mqtt.php

    'host'      => env('mqtt_host','127.0.0.1'),
    'password'  => env('mqtt_password',''),
    'username'  => env('mqtt_username',''),
    'certfile'  => env('mqtt_cert_file',''),
    'localcert' => env('mqtt_local_cert', ''),
    'localpk'   => env('mqtt_local_pk', ''),
    'port'      => env('mqtt_port','1883'),
    'debug'     => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True
    'qos'       => env('mqtt_qos', 0), // set quality of service here
    'retain'    => env('mqtt_retain', 0) // it should be 0 or 1 Whether the message should be retained.- Retain Flag

发布主题

use Elemenx\Mqtt\MqttClass\Mqtt;

public function sendMsgViaMqtt($topic, $message)
{
    $mqtt = new Mqtt();
    $client_id = Auth::user()->id;
    $output = $mqtt->connectAndPublish($topic, $message, $client_id);

    if ($output === true)
    {
        return "published";
    }
    
    return "Failed";
}

使用外观发布主题

use Mqtt;

public function sendMsgViaMqtt($topic, $message)
{
    $client_id = Auth::user()->id;
    
    $output = Mqtt::connectAndPublish($topic, $message, $client_id);

    if ($output === true)
    {
        return "published";
    }

    return "Failed";
}

订阅主题

use Elemenx\Mqtt\MqttClass\Mqtt;

public function subscribetoTopic($topic)
{
    $mqtt = new Mqtt();
    $client_id = Auth::user()->id;
    $mqtt->connectAndSubscribe($topic, function($topic, $msg){
        echo "Msg Received: \n";
        echo "Topic: {$topic}\n\n";
        echo "\t$msg\n\n";
    }, $client_id);
}

使用外观订阅主题

use Mqtt;

public function subscribetoTopic($topic)
{
    //You can also subscribe to multiple topics using the same function $topic can be array of topics e.g ['topic1', 'topic2']

    Mqtt::connectAndSubscribe($topic, function($topic, $msg){
        echo "Msg Received: \n";
        echo "Topic: {$topic}\n\n";
        echo "\t$msg\n\n";
    },$client_id);
}

使用辅助方法发布主题


public function sendMsgViaMqtt($topic, $message)
{
    $client_id = Auth::user()->id;
    
    $output = connectToPublish($topic, $message, $client_id);

    if ($output === true)
    {
        return "published";
    }

    return "Failed";
}

使用辅助方法订阅主题

//You can also subscribe to multiple topics using the same function $topic can be array of topics e.g ['topic1', 'topic2']
public function subscribetoTopic($topic)
{
    return connectToSubscribe($topic, $client_id);
}

快乐编码...!