Laravel Mqtt Broker package
This commit is contained in:
commit
5d9ea776a2
6 files changed
+489
No files matched your search
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: salman
|
||||
* Date: 2/22/19
|
||||
* Time: 1:16 PM
|
||||
*/
|
||||
|
||||
namespace Salman\Mqtt\MqttClass;
|
||||
|
||||
class Mqtt
|
||||
{
|
||||
protected $client;
|
||||
protected $host = null;
|
||||
protected $username = null;
|
||||
protected $cert_file = null;
|
||||
protected $password = null;
|
||||
protected $port = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->host = config('mqtt.host');
|
||||
$this->username = config('mqtt.username');
|
||||
$this->password = config('mqtt.password');
|
||||
$this->cert_file = config('mqtt.certfile');
|
||||
$this->port = config('mqtt.port');
|
||||
|
||||
// $this->client = new phpMQTT($this->host, $this->port, 25,$this-$this->cert_file);
|
||||
// $this->client = new MQTTClient($this->host,$this->port);
|
||||
}
|
||||
|
||||
|
||||
public function ConnectAndSendMessage($topic, $msg)
|
||||
{
|
||||
$client = new phpMQTT($this->host,$this->port, rand(0,100), $this->cert_file);
|
||||
|
||||
if ($client->connect(true))
|
||||
{
|
||||
$client->publish($topic,$msg);
|
||||
$client->close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
// $this->client->setAuthentication($this->username,$this->password);
|
||||
// $this->client->setEncryption($this->cert_file);
|
||||
// $success = $this->client->sendConnect(rand(0,63)); // set your client ID
|
||||
// if ($success) {
|
||||
// $this->client->sendPublish($topic, $msg);
|
||||
// $messages = $this->client->getPublishMessages(); // now read and acknowledge all messages waiting
|
||||
// foreach ($messages as $message) {
|
||||
// echo $message['topic'] .': '. $message['message'] . PHP_EOL;
|
||||
// }
|
||||
//
|
||||
// $this->client->sendDisconnect();
|
||||
// echo 'success';
|
||||
// }
|
||||
//
|
||||
// $this->client->close();
|
||||
// echo 'error';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user