Skip to content

Commit d08f6b9

Browse files
committed
Add wildcard example
1 parent d268ef9 commit d08f6b9

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

examples/wildcard.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* This file is part of Simps.
4+
*
5+
* @link https://github.com/simps/mqtt
6+
* @contact Lu Fei <lufei@simps.io>
7+
*
8+
* For the full copyright and license information,
9+
* please view the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
include_once __DIR__ . '/bootstrap.php';
13+
14+
use Simps\MQTT\Client;
15+
use Simps\MQTT\Protocol\Types;
16+
use Swoole\Coroutine;
17+
18+
Coroutine\run(function () {
19+
$client = new Client(SIMPS_MQTT_REMOTE_HOST, SIMPS_MQTT_PORT, getTestConnectConfig());
20+
$will = [
21+
'topic' => 'simps-mqtt/users/byebye',
22+
'qos' => 0,
23+
'retain' => 0,
24+
'message' => 'byebye',
25+
];
26+
$client->connect(true, $will);
27+
$topics['simps-mqtt/users/#'] = 0;
28+
$client->subscribe($topics);
29+
$timeSincePing = time();
30+
while (true) {
31+
try {
32+
$buffer = $client->recv();
33+
if ($buffer && $buffer !== true) {
34+
var_dump($buffer);
35+
// QoS1 PUBACK
36+
if ($buffer['type'] === Types::PUBLISH && $buffer['qos'] === 1) {
37+
$client->send(
38+
[
39+
'type' => Types::PUBACK,
40+
'message_id' => $buffer['message_id'],
41+
],
42+
false
43+
);
44+
}
45+
if ($buffer['type'] === Types::DISCONNECT) {
46+
echo "Broker is disconnected\n";
47+
$client->close();
48+
break;
49+
}
50+
}
51+
if ($timeSincePing <= (time() - $client->getConfig()->getKeepAlive())) {
52+
$buffer = $client->ping();
53+
if ($buffer) {
54+
echo 'send ping success' . PHP_EOL;
55+
$timeSincePing = time();
56+
}
57+
}
58+
} catch (\Throwable $e) {
59+
throw $e;
60+
}
61+
}
62+
});

0 commit comments

Comments
 (0)