Skip to content

Commit

Permalink
Add wildcard example
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Aug 8, 2024
1 parent d268ef9 commit d08f6b9
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions examples/wildcard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
});

0 comments on commit d08f6b9

Please sign in to comment.