Skip to content

Commit 4ce8eba

Browse files
committed
first commit
0 parents  commit 4ce8eba

File tree

7 files changed

+868
-0
lines changed

7 files changed

+868
-0
lines changed

LICENSE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#laravel-mqtt
2+
3+
A simple Laravel MQTT client provides Publish/Subscribe methods.
4+
5+
Source code is mainly based on [phpMQTT](https://github.com/bluerhinos/phpMQTT)
6+
7+
Once the configuration is complete, it is ready to use.
8+

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "jzzoo/laravel-mqttx",
3+
"description": "mqtt client",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "jzzoo",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.0"
14+
},
15+
"require-dev": {
16+
"laravel/framework": ">=5.8"
17+
},
18+
"minimum-stability": "dev",
19+
"prefer-stable": true,
20+
"autoload": {
21+
"psr-4": {
22+
"Jzzoo\\Laravel\\Mqttx\\": "src/"
23+
}
24+
},
25+
"extra": {
26+
"laravel": {
27+
"providers": [
28+
"Jzzoo\\Laravel\\Mqttx\\MqttxProvider"
29+
],
30+
"aliases": {
31+
"Mqtt": "Jzzoo\\Laravel\\Mqttx\\Facades\\Mqttx"
32+
}
33+
}
34+
}
35+
}

config/mqttx.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'host' => env('mqtt_host','127.0.0.1'),
5+
'password' => env('mqtt_password',''),
6+
'username' => env('mqtt_username',''),
7+
'certfile' => env('mqtt_cert_file',''),
8+
'port' => env('mqtt_port','1883'),
9+
'debug' => env('mqtt_debug',false),
10+
'qos' => env('mqtt_qos', 0),
11+
'retain' => env('mqtt_retain', 0)
12+
];

src/Facades/Mqttx.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Jzzoo\Laravel\Mqttx;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Mqttx extends Facade
8+
{
9+
protected static function getFacadeAccessor()
10+
{
11+
return 'Mqttx';
12+
}
13+
}

src/Mqttx.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Jzzoo\Laravel\Mqttx;
4+
5+
class Mqttx
6+
{
7+
public function __construct() {
8+
$this->host = config('mqtt.host');
9+
$this->username = config('mqtt.username');
10+
$this->password = config('mqtt.password');
11+
$this->cert_file = config('mqtt.certfile');
12+
$this->port = config('mqtt.port');
13+
$this->debug = config('mqtt.debug');
14+
$this->qos = config('mqtt.qos');
15+
$this->retain = config('mqtt.retain');
16+
}
17+
18+
/**
19+
* 发布消息
20+
*
21+
* @param $topic
22+
* @param $msg
23+
* @param null $client_id
24+
* @return bool
25+
*/
26+
public function Publish($topic, $msg, $client_id = null) {
27+
28+
empty($client_id) && $client_id = rand(10000, 99999);
29+
30+
$mqtt = new MqttxNear($this->host,$this->port, $client_id, $this->cert_file, $this->debug);
31+
32+
if ( $mqtt->connect(true, null, $this->username, $this->password) ) {
33+
$mqtt->publish($topic, $msg, $this->qos, $this->retain);
34+
$mqtt->close();
35+
return true;
36+
}
37+
38+
return false;
39+
}
40+
41+
/**
42+
* 订阅消息
43+
*
44+
* @param $topic
45+
* @param $function
46+
* @param null $client_id
47+
* @return bool
48+
*/
49+
public function Subscribe($topic, $function, $client_id = null) {
50+
51+
empty($client_id) && $client_id = mt_rand(10000, 99999);
52+
53+
$mqtt = new MqttxNear($this->host,$this->port, $client_id);
54+
if( $mqtt->connect(true, null, $this->username, $this->password) ) {
55+
56+
$topics[$topic] = [ 'qos' => 0, 'function' => $function ];
57+
$mqtt->subscribe($topics, 0);
58+
59+
do {
60+
$rs = $mqtt->proc();
61+
} while($rs);
62+
63+
$mqtt->close();
64+
return true;
65+
}
66+
return false;
67+
}
68+
}

0 commit comments

Comments
 (0)