-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d268ef9
commit d08f6b9
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}); |