PHP code example of imiphp / imi-mqtt
1. Go to this page and download the library: Download imiphp/imi-mqtt library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
imiphp / imi-mqtt example snippets
[
'components' => [
'MQTT' => 'Imi\MQTT',
],
]
'mainServer' => [
'namespace' => 'ImiApp\MQTTServer',
'type' => 'MQTTServer',
'host' => '127.0.0.1',
'port' => 8081,
'controller' => \ImiApp\MQTTServer\Controller\MQTTController::class,
// 'configs' => [
// // 启用 MQTTS 配置证书
// 'ssl_cert_file' => dirname(__DIR__) . '/ssl/server.crt',
// 'ssl_key_file' => dirname(__DIR__) . '/ssl/server.key',
// ],
],
// 子服务器(端口监听)配置
'subServers' => [
'xxxServer' => [
'namespace' => 'ImiApp\MQTTServer',
'type' => 'MQTTServer',
'host' => '127.0.0.1',
'port' => 8081,
'controller' => \ImiApp\MQTTServer\Controller\MQTTController::class,
// 'configs' => [
// // 启用 MQTTS 配置证书
// 'ssl_cert_file' => dirname(__DIR__) . '/ssl/server.crt',
// 'ssl_key_file' => dirname(__DIR__) . '/ssl/server.key',
// ],
],
],
namespace Imi\MQTT\Test;
use Imi\MQTT\Client\Contract\IMQTTClientListener;
class TestClientListener implements IMQTTClientListener
{
/**
* 连接确认
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\ConnectResponsePacket $packet
* @return void
*/
public function connectACK(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\ConnectResponsePacket $packet)
{
}
/**
* 发布
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\PublishRequestPacket $packet
* @return void
*/
public function publish(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\PublishRequestPacket $packet)
{
}
/**
* 发布确认
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\PublishAckPacket $packet
* @return void
*/
public function publishAck(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\PublishAckPacket $packet)
{
}
/**
* 发布已收到(保证交付部分1)
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\PublishReceivedPacket $packet
* @return void
*/
public function publishReceived(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\PublishReceivedPacket $packet)
{
}
/**
* 发布释放(确保交付的第2部分)
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\PublishReleasePacket $packet
* @return void
*/
public function publishRelease(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\PublishReleasePacket $packet)
{
}
/**
* 发布完成(保证交付的第3部分)
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\PublishCompletePacket $packet
* @return void
*/
public function publishComplete(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\PublishCompletePacket $packet)
{
}
/**
* 订阅确认
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\SubscribeResponsePacket $packet
* @return void
*/
public function subscribeACK(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\SubscribeResponsePacket $packet)
{
}
/**
* 取消订阅确认
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\UnsubscribeResponsePacket $packet
* @return void
*/
public function unsubscribeACK(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\UnsubscribeResponsePacket $packet)
{
}
/**
* Ping 响应
*
* @param \Imi\MQTT\Client\MQTTClient $client
* @param \BinSoul\Net\Mqtt\Packet\PingResponsePacket $packet
* @return void
*/
public function ping(\Imi\MQTT\Client\MQTTClient $client, \BinSoul\Net\Mqtt\Packet\PingResponsePacket $packet)
{
}
}
use Imi\MQTT\Client\MQTTClient;
$client = new MQTTClient([
'host' => '127.0.0.1',
'port' => 8081,
], new TestClientListener);
$client->wait(); // 开始循环接收,直到关闭连接