From 344f501811ba37a287c22205e007b5153b4fbfc8 Mon Sep 17 00:00:00 2001 From: Salman Zafar Date: Mon, 15 Jul 2019 15:39:48 +0500 Subject: [PATCH] added client id of users choice feature --- Readme.md | 14 ++++++++++---- src/MqttClass/Mqtt.php | 12 ++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Readme.md b/Readme.md index 1a4ab84..65b0043 100644 --- a/Readme.md +++ b/Readme.md @@ -15,8 +15,10 @@ composer require salmanzafar/laravel-mqtt * Name and Password Authentication * Certificate Protection for end to end encryption * Enable Debug mode to make it easier for debugging +* Now you can also set Client_id of your choice and if you don't want just simply don't use or set it to null ## Enable the package (Optional) + This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel >= 5.5. __This step is only required if you are using laravel version <5.5__ @@ -58,7 +60,8 @@ use Salman\Mqtt\MqttClass\Mqtt; public function SendMsgViaMqtt($topic, $message) { $mqtt = new Mqtt(); - $output = $mqtt->ConnectAndPublish($topic, $message); + $client_id = Auth::user()->id; + $output = $mqtt->ConnectAndPublish($topic, $message, $client_id); if ($output === true) { @@ -75,7 +78,9 @@ use Mqtt; public function SendMsgViaMqtt($topic, $message) { - $output = Mqtt::ConnectAndPublish($topic, $message); + $client_id = Auth::user()->id; + + $output = Mqtt::ConnectAndPublish($topic, $message, $client_id); if ($output === true) { @@ -94,11 +99,12 @@ use Salman\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); } @@ -114,7 +120,7 @@ public function SubscribetoTopic($topic) echo "Msg Received: \n"; echo "Topic: {$topic}\n\n"; echo "\t$msg\n\n"; - }); + },$client_id); } diff --git a/src/MqttClass/Mqtt.php b/src/MqttClass/Mqtt.php index 421e729..6309fe2 100644 --- a/src/MqttClass/Mqtt.php +++ b/src/MqttClass/Mqtt.php @@ -53,9 +53,11 @@ class Mqtt } - public function ConnectAndPublish($topic, $msg) + public function ConnectAndPublish($topic, $msg, $client_id=null) { - $client = new MqttService($this->host,$this->port, rand(0,100), $this->cert_file, $this->debug); + $id = empty($client_id) ? rand(0,999) : $client_id; + + $client = new MqttService($this->host,$this->port, $id, $this->cert_file, $this->debug); if ($client->connect(true, null, $this->username, $this->password)) { @@ -69,9 +71,11 @@ class Mqtt } - public function ConnectAndSubscribe($topic, $proc) + public function ConnectAndSubscribe($topic, $proc, $client_id=null) { - $client = new MqttService($this->host,$this->port, rand(0,100), $this->cert_file, $this->debug); + $id = empty($client_id) ? rand(0,999) : $client_id; + + $client = new MqttService($this->host,$this->port,$id, $this->cert_file, $this->debug); if ($client->connect(true, null, $this->username, $this->password)) {