本文采用[Docker]方式来搭建,对于[Docker]的使用或者一些知识有另一篇文章(暂未写好)来介绍,这里仅说明如何使用[Docker]进行[MQTT]的搭建。wonmodeair:~ wonmo$ docker search swoole
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
phpswoole/swoole Official Docker image for PHP Swoole : https… 75
twosee/swoole-coroutine Perfect Swoole Dockerfile for senior develop… 44 [OK]
twosee/swoole Perfect Swoole Dockerfile for senior develop… 14 [OK]
kakuilan/swoole-docker php7 swoole docker 2
这里选取了第一个镜像,现在将镜像下载下来。
docker pull phpswoole/swoole
下载过程就不贴了,主要是一些镜像层的下载合并。然后根据这个镜像启动一个容器。
docker run --rm -t -i --name swoole -p 9503:9503 -v Users/wonmo/Desktop/docker_volumes/swoole:/var/www phpswoole/swoole:latest bin/bash
这里要映射一个本地目录到容器里,命令的话会专门写一篇[Docker]文章进行解释。像这样[Swoole]的环境就搭建好了,剩下的我们需要进行[MQTT]包的安装。
composer create-project simple-swoole/skeleton swoole
在容器里运行上述命令就会在本地目录创建一个[Swoole]文件夹,里面包含了一个简单的[WebSocket]实现例子,这里由于需要进行[MQTT]的实现,所以需要增加一些代码。
在增加代码前我们先执行以下命令
composer require simps/mqtt
打开本地目录下的[.../swoole/simps/config/servers.php]文件,增加如下代码。
use Simps\Server\Protocol\MQTT; #注意别忘了命名空间
'mqtt' => [
'ip' => '0.0.0.0',
'port' => 9503,
'callbacks' => [
],
'receiveCallbacks' => [
MQTT::COnNECT=> [\App\Events\MqttServer::class, 'onMqConnect'],
MQTT::PINGREQ => [\App\Events\MqttServer::class, 'onMqPingreq'],
MQTT::DISCOnNECT=> [\App\Events\MqttServer::class, 'onMqDisconnect'],
MQTT::PUBLISH => [\App\Events\MqttServer::class, 'onMqPublish'],
MQTT::SUBSCRIBE => [\App\Events\MqttServer::class, 'onMqSubscribe'],
MQTT::UNSUBSCRIBE => [\App\Events\MqttServer::class, 'onMqUnsubscribe'],
],
'settings' => [
'worker_num' => 1,
'open_mqtt_protocol' => true,
],
],
然后在[Events]文件夹增加[MqttServer.php]文件。
declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/
namespace App\Events;
use Simps\Server\Protocol\MQTT;
use Simps\Server\Protocol\MqttInterface;
class MqttServer implements MqttInterface
{
public function onMqConnect($server, int $fd, $fromId, $data)
{
if ($data['protocol_name'] != "MQTT") {
// 如果协议名不正确服务端可以断开客户端的连接,也可以按照某些其它规范继续处理CONNECT报文
$server->close($fd);
return false;
}
$server->send(
$fd,
MQTT::getAck(
[
'cmd' => 2, // CONNACK固定值为2
'code' => 0, // 连接返回码 0表示连接已被服务端接受
'session_present' => 0
]
)
);
}
public function onMqPingreq($server, int $fd, $fromId, $data): bool{
return true;
}
public function onMqDisconnect($server, int $fd, $fromId, $data): bool{
return true;
}
public function onMqPublish($server, int $fd, $fromId, $data){
var_dump($data);
}
public function onMqSubscribe($server, int $fd, $fromId, $data){}
public function onMqUnsubscribe($server, int $fd, $fromId, $data){}
}
开启[Swoole]服务
php bin/simps.php mqtt:start
root@3064ee6c19e8:/var/www/simps# php bin/simps.php mqtt:start
____ _
___| (_) _ __ ___ _ __ ___
\___ \ | | | '_ ` _ \ | '_ \ __|
___) | | | | | | | | | | |_) | \__ \
|____/ |_| |_| |_| |_| | .__/ |___/
|_| Version: 1.0.5, Swoole: 4.7.1
[2021-08-21 13:32:17] [INFO] Swoole MQTT Server running:mqtt://0.0.0.0:9503
开启成功以后可以使用[MQTT]相关的工具进行连接测试,这里我用了[MQTT.FX]。网上有推荐[MQTTBox]的,但是我没有找到下载地址,然后去[GitHub]上下载了一份源码,编译了一下。发现是个[Web App]不支持[MQTT]。
MQTT over TCP and TLS are supported by MQTTBox apps for Linux, MAC and Windows. Download MQTTBox app from http://workswithweb.com/mqttbox.html
[MQTT.FX]还是比较简单的,进行配置以后发送个简单的文本。
root@3064ee6c19e8:/var/www/simps# php bin/simps.php mqtt:start
____ _
___| (_) _ __ ___ _ __ ___
\___ \ | | | '_ ` _ \ | '_ \ / __|
___) | | | | | | | | | | |_) | \__ \
|____/ |_| |_| |_| |_| | .__/ |___/
|_| Version: 1.0.5, Swoole: 4.7.1
[2021-08-21 13:39:48] [INFO] Swoole MQTT Server running:mqtt://0.0.0.0:9503
array(6) {
["cmd"]=>
int(3)
["topic"]=>
string(12) "上下博客"
["content"]=>
string(0) ""
["dup"]=>
int(0)
["qos"]=>
int(0)
["retain"]=>
int(0)
}
此篇文章仅仅只是搭建,如果需要知道[MQTT]详细的内容,请关注后续。
... 未完待续 ...