使用webman的自定义进程,启动mqtt客户端报错
//webman进程文件
return [
//省略框架自带的配置
'websocket_test' => [
'handler' => \app\service\mqttClient::class,
],
];
//mqtt客户端
<?php
namespace app\service;
use Workerman\Mqtt\Client;
class mqttClient
{
public function onWorkerStart()
{
$options = [
'client_id' => '"MqttClient_'.strval(round(microtime(true) * 1000)),
'username' => '***',
'password' => '********',
'ssl' => true,
'debug' => true,
];
$mqtt = new Client('websocket://s.sportnanoapi.com:443', $options);
$mqtt->onConnect = function($mqtt) {
$mqtt->subscribe('test', ['qos'=>1]);
};
$mqtt->onError = function ($e) {
echo "错误信息".$e->getMessage()."\n";
};
$mqtt->onMessage = function($topic, $content){
echo "topic:$topic content:$content\n";
};
$mqtt->connect();
}
}
-> Try to connect to websocket://s.sportnanoapi.com:443
-- Tcp connection established
-> Send CONNECT package client_id:"MqttClient_1704030527628 username:*** password:*** clean_session:1 protocol_name:MQTT protocol_level:4
<- Recv PUBACK package, message_id:21584
buffer:636861727365743d5554462d380d0a436f6e74656e742d4c656e6774683a2031 lenth:25448 not enough for unpackString
ErrorException: unpack(): Type n: not enough input, need 2, have 0 in /home/www/new_webman/vendor/workerman/mqtt/src/Protocols/Mqtt.php:450
Stack trace:
#0 [internal function]: {closure}()
#1 /home/www/new_webman/vendor/workerman/mqtt/src/Protocols/Mqtt.php(450): unpack()
#2 /home/www/new_webman/vendor/workerman/mqtt/src/Protocols/Mqtt.php(301): Workerman\Mqtt\Protocols\Mqtt::readShortInt()
#3 /home/www/new_webman/vendor/workerman/workerman/Connection/TcpConnection.php(646): Workerman\Mqtt\Protocols\Mqtt::decode()
#4 /home/www/new_webman/vendor/workerman/workerman/Events/Select.php(311): Workerman\Connection\TcpConnection->baseRead()
#5 /home/www/new_webman/vendor/workerman/workerman/Worker.php(1635): Workerman\Events\Select->loop()
#6 /home/www/new_webman/vendor/workerman/workerman/Worker.php(1426): Workerman\Worker::forkOneWorkerForLinux()
#7 /home/www/new_webman/vendor/workerman/workerman/Worker.php(1400): Workerman\Worker::forkWorkersForLinux()
#8 /home/www/new_webman/vendor/workerman/workerman/Worker.php(560): Workerman\Worker::forkWorkers()
#9 /home/www/new_webman/vendor/workerman/webman-framework/src/support/App.php(131): Workerman\Worker::runAll()
#10 /home/www/new_webman/start.php(4): support\App::run()
#11 {main}
这里写具体的系统环境相关信息
php=8.2。webman=1.5.13。workerman/mqtt=1.5
mqtt客户端默认不支持websocket。
不过有开发者实现了,见 https://www.workerman.net/a/1466