Phpmqtt v1.3.0 release, mqtt protocol analysis & coroutine client

Time:2021-6-12

In v1.3.0, a new message class cluster is added, which is mainly used to reply to peer ack in the server.


use Simps\MQTT\Protocol\Types;
use Simps\MQTT\Protocol\V3;
use Simps\MQTT\Message\ConnAck;
use Simps\MQTT\Message\PingResp;

$server = new Swoole\Server('127.0.0.1', 1883, SWOOLE_BASE);

$server->set(
    [
        'open_mqtt_protocol' => true,
        'package_max_length' => 2 * 1024 * 1024,
    ]
);

$server->on('connect', function ($server, $fd) {
    echo "Client #{$fd}: Connect.\n";
});

$server->on('receive', function (Swoole\Server $server, $fd, $from_id, $data) {
    $data = V3::unpack($data);
    if (is_array($data) && isset($data['type'])) {
        switch ($data['type']) {
            case Types::CONNECT:
                if ($data['protocol_name'] != 'MQTT') {
                    $server->close($fd);

                    return false;
                }
                $server->send(
                    $fd,
                    (new ConnAck())->setCode(0)
                        ->setSessionPresent(0)
                );
                break;
            case Types::PINGREQ:
                $server->send($fd, (new PingResp()));
                break;
        }
    } else {
        $server->close($fd);
    }
});

$server->on('close', function ($server, $fd) {
    echo "Client #{$fd}: Close.\n";
});

$server->start();

Received atCONNECTAfter the package, you need to replyCONNACKThe previous version needs to be called manually by the userpackClasssend_data

Now you just need to instantiate the corresponding message class and set the required value, such as connack:

  • MQTT3
use Simps\MQTT\Message\ConnAck;

$ack = new ConnAck();
$ack->setCode(0)->setSessionPresent(0);

$server->send($fd, $ack->getContents());
$server->send($fd, $ack);
  • MQTT5
use Simps\MQTT\Message\ConnAck;
use Simps\MQTT\Protocol\ProtocolInterface;

$ack = new ConnAck();
$ack->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0)
    ->setCode(0)
    ->setSessionPresent(0)
    ->setProperties([]);

$server->send($fd, $ack->getContents());
$server->send($fd, $ack);

Others can be viewedSpecific classesperhapsexamplefile

Update log

Downward incompatibility

  • Suback’spayloadShould be a return code, modify the key namepayloadbycodes (9e72ce2) (283ff41)

enhance

  • Optimize client recv (# 38) (99a85bf)
  • Add connack & Publish & pingresp message (700a6c9)
  • Add suback message and update GetMessage ID (09f6334)
  • Add disconnect, puback, pubrec, pubrel, unsuback (20a78c7)
  • Modify abstractconfig (dff6283)
  • Add message usage example (58d5b4a)
  • Add getcontents to facilitate__toStringCall in (a7ba577)
  • Add message usage document (bab2297)

About phpmqtt

  • Mqtt protocol analysis & coroutine client
  • Mqtt protocol parsing and coroutine client for PHP
  • Support mqtt protocol version 3.1, 3.1.1 and 5.0, support QoS 0, QoS 1 and QoS 2
  • The first PHP library supporting mqtt V5.0 protocol

file:https://mqtt.simps.io
GitHub:https://github.com/simps/mqtt
Gitee:https://gitee.com/phpiot/mqtt

Support, remember to order a star~

Phpmqtt v1.3.0 release, mqtt protocol analysis & coroutine client

Recommended Today

Usage of unity avprovider plug-in

1. Create a new mediaplayer component 2. Create a new avprovideo component under canvas And assign the mediaplayer component created in the previous step to the mediaplayer on the avprovider component 3. Put the video to be played in the streamingassets folder The next step is to call with code 1、_ mediaPlayer. Openvideofromfile (mediaplayer. Filelocation. […]