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