Skip to content

Commit c1cf376

Browse files
committed
init
0 parents  commit c1cf376

26 files changed

+2046
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
.vscode

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"bluerhinos/phpmqtt": "@dev"
4+
}
5+
}

composer.lock

+68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* @Descripttion:
4+
* @Author: ovim <[email protected]>
5+
* @Date: 2021/9/24 3:10 下午
6+
*/
7+
8+
error_reporting(E_ALL);
9+
10+
require_once 'vendor/autoload.php';
11+
12+
function d($data) {
13+
echo "<pre>";
14+
var_dump($data);
15+
}
16+
17+
function echoDie($data) {
18+
print_r($data);
19+
die;
20+
}
21+
22+
class Test {
23+
24+
/**
25+
* @var string
26+
*/
27+
protected $cliendId;
28+
29+
/**
30+
* @var null
31+
*/
32+
protected $broker;
33+
34+
public function __construct($cliendId)
35+
{
36+
$this->cliendId = $cliendId;
37+
38+
$this->connect();
39+
}
40+
41+
private function connect() {
42+
list($address, $port, $clientId, $username, $password) = [
43+
'ip for server',
44+
'port',
45+
$this->cliendId,
46+
'username',
47+
'password'
48+
];
49+
50+
$broker = new Bluerhinos\phpMQTT($address, $port, $clientId);
51+
$connectResult = $broker->connect(true, null, $username, $password);
52+
if (!$connectResult) echoDie('EMQ 服务端连接失败');
53+
$this->broker = $broker;
54+
}
55+
56+
/**
57+
* publish message
58+
*
59+
* @param $topic
60+
* @param $content
61+
* @param int $qos
62+
* @param bool $retain
63+
*/
64+
public function pub($topic, $content, $qos = 0, $retain = true)
65+
{
66+
$result = $this->broker->publish($topic, $content, $qos, $retain);
67+
d($result);
68+
}
69+
70+
/**
71+
* subscribe message
72+
*
73+
* @param $topic
74+
* @param int $qos
75+
*/
76+
public function sub($topic, $qos = 0)
77+
{
78+
$result = $this->broker->subscribeAndWaitForMessage($topic, $qos);
79+
// $result = $this->broker->subscribe($topic, $qos);
80+
// 这个地方如果获取到脏数据 如何处理【 订阅之前,向此主题发送一个空的消息,清空保留消息 】
81+
d($result);
82+
}
83+
84+
}
85+
86+
$clientId = mt_rand();
87+
$topic = 'test';
88+
(new Test($clientId))->pub($topic, 'ovimTest1');
89+
(new Test($clientId))->sub($topic);

readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
EMQ X 的 PHP SDK 使用DEMO
2+
3+
[查看 EMQ 的官方文档](https://docs.emqx.cn/broker/v4.3/)

vendor/autoload.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer/autoload_real.php';
6+
7+
return ComposerAutoloaderInit155a8ed16bd5ee7ecef74e126f5bf068::getLoader();

vendor/bluerhinos/phpmqtt/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
.idea

vendor/bluerhinos/phpmqtt/LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2010 Blue Rhinos Consulting | Andrew Milsted
2+
[email protected] | http://www.bluerhinos.co.uk
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

vendor/bluerhinos/phpmqtt/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**2020 Update**
2+
I am restarting the project so watch this space
3+
4+
5+
Blue Rhinos Consulting
6+
Andrew Milsted | [email protected] | http://www.bluerhinos.co.uk | @bluerhinos
7+
8+
A simple php class to connect/publish/subscribe to an MQTT broker
9+
10+
Documentation: Coming Soon
11+
Source: http://github.com/bluerhinos/phpMQTT
12+
13+
To install via Composer
14+
-----------------------
15+
`composer require bluerhinos/phpmqtt=@dev`
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "bluerhinos/phpmqtt",
3+
"description": "Simple MQTT Class",
4+
"license": "BSD",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Andrew Milsted",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "dev",
13+
"require": {
14+
"php": ">=7.2"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Bluerhinos\\": "./"
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Output example
2+
3+
For info on MQTT: https://mntolia.com/fundamentals-mqtt/#4_Advantages_of_MQTT_for_IoT_over_HTTP_UDP
4+
5+
> Before running each file be sure to review the four connection parameters in the headers.
6+
7+
subscribe.php
8+
--
9+
This example is to demonstrate using MQTT for a long-running script that will wait for subscribed topics.
10+
This script is not suitable to be run as web requests, instead should be run on the commandline.
11+
12+
Let `subscribe.php` listening the broker:
13+
```console
14+
$ php subscribe.php
15+
Msg Recieved: Fri, 13 Jan 2017 01:58:23 +0000
16+
Topic: bluerhinos/phpMQTT/examples/publishtest
17+
18+
Hello World! at Fri, 13 Jan 2017 01:58:23 +0000
19+
20+
Msg Recieved: Fri, 13 Jan 2017 01:58:35 +0000
21+
Topic: bluerhinos/phpMQTT/examples/publishtest
22+
23+
Hello World! at Fri, 13 Jan 2017 01:58:35 +0000
24+
25+
^C
26+
```
27+
28+
publish.php
29+
---
30+
This example will publish a message to a topic.
31+
32+
The results shown above corresponds to publisher's two actions:
33+
```console
34+
$ php publish.php
35+
$ php publish.php
36+
```
37+
38+
When run as a web request you it is ok to just `$mqtt->connect()`, `$mqtt->publish()` and `$mqtt->close()`.
39+
If it is being run as long-running command line script you should run `$mqtt->proc()` regularly in order maintain the connection with the broker.
40+
41+
subscribeAndWaitForMessage.php
42+
--
43+
In order to use this library to display messages on a website you can use `$mqtt->subscribeAndWaitForMessage()`, this will subscribe to a topic and then wait for, and return the message.
44+
If you want messages to appear instantly, you should use retained messages (https://mntolia.com/mqtt-retained-messages-explained-example/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require('../phpMQTT.php');
4+
5+
$server = 'localhost'; // change if necessary
6+
$port = 1883; // change if necessary
7+
$username = ''; // set your username
8+
$password = ''; // set your password
9+
$client_id = 'phpMQTT-publisher'; // make sure this is unique for connecting to sever - you could use uniqid()
10+
11+
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
12+
13+
if ($mqtt->connect(true, NULL, $username, $password)) {
14+
$mqtt->publish('bluerhinos/phpMQTT/examples/publishtest', 'Hello World! at ' . date('r'), 0, false);
15+
$mqtt->close();
16+
} else {
17+
echo "Time out!\n";
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
require('../phpMQTT.php');
4+
5+
6+
$server = 'localhost'; // change if necessary
7+
$port = 1883; // change if necessary
8+
$username = ''; // set your username
9+
$password = ''; // set your password
10+
$client_id = 'phpMQTT-subscriber'; // make sure this is unique for connecting to sever - you could use uniqid()
11+
12+
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
13+
if(!$mqtt->connect(true, NULL, $username, $password)) {
14+
exit(1);
15+
}
16+
17+
$mqtt->debug = true;
18+
19+
$topics['bluerhinos/phpMQTT/examples/publishtest'] = array('qos' => 0, 'function' => 'procMsg');
20+
$mqtt->subscribe($topics, 0);
21+
22+
while($mqtt->proc()) {
23+
24+
}
25+
26+
$mqtt->close();
27+
28+
function procMsg($topic, $msg){
29+
echo 'Msg Recieved: ' . date('r') . "\n";
30+
echo "Topic: {$topic}\n\n";
31+
echo "\t$msg\n\n";
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require('../phpMQTT.php');
4+
5+
$server = 'localhost'; // change if necessary
6+
$port = 1883; // change if necessary
7+
$username = ''; // set your username
8+
$password = ''; // set your password
9+
$client_id = 'phpMQTT-subscribe-msg'; // make sure this is unique for connecting to sever - you could use uniqid()
10+
11+
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
12+
if(!$mqtt->connect(true, NULL, $username, $password)) {
13+
exit(1);
14+
}
15+
16+
echo $mqtt->subscribeAndWaitForMessage('bluerhinos/phpMQTT/examples/publishtest', 0);
17+
18+
$mqtt->close();

0 commit comments

Comments
 (0)