-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
wildcard.php
62 lines (59 loc) · 1.85 KB
/
wildcard.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
<?php
/**
* This file is part of Simps.
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
include_once __DIR__ . '/bootstrap.php';
use Simps\MQTT\Client;
use Simps\MQTT\Protocol\Types;
use Swoole\Coroutine;
Coroutine\run(function () {
$client = new Client(SIMPS_MQTT_REMOTE_HOST, SIMPS_MQTT_PORT, getTestConnectConfig());
$will = [
'topic' => 'simps-mqtt/users/byebye',
'qos' => 0,
'retain' => 0,
'message' => 'byebye',
];
$client->connect(true, $will);
$topics['simps-mqtt/users/#'] = 0;
$client->subscribe($topics);
$timeSincePing = time();
while (true) {
try {
$buffer = $client->recv();
if ($buffer && $buffer !== true) {
var_dump($buffer);
// QoS1 PUBACK
if ($buffer['type'] === Types::PUBLISH && $buffer['qos'] === 1) {
$client->send(
[
'type' => Types::PUBACK,
'message_id' => $buffer['message_id'],
],
false
);
}
if ($buffer['type'] === Types::DISCONNECT) {
echo "Broker is disconnected\n";
$client->close();
break;
}
}
if ($timeSincePing <= (time() - $client->getConfig()->getKeepAlive())) {
$buffer = $client->ping();
if ($buffer) {
echo 'send ping success' . PHP_EOL;
$timeSincePing = time();
}
}
} catch (\Throwable $e) {
throw $e;
}
}
});