使用workerman/mqtt报错问题,请老师帮下忙,谢谢

kenshin100

问题描述

使用官方代码里的程序,仅仅修改了URL,我用mqttfx,并且配置好后是可以连接mqtt服务端,但是用PHP程序却一直报这个错,请老师帮忙看一下

程序代码

<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
    $mqtt = new Workerman\Mqtt\Client('mqtt://localhost:1883');
    $mqtt->onConnect = function($mqtt) {
        $mqtt->subscribe('test');
    };
    $mqtt->onMessage = function($topic, $content){
        var_dump($topic, $content);
    };
    $mqtt->connect();
};
Worker::runAll();

报错信息

---------------------------------------------- WORKERMAN -----------------------------------------------
Workerman version:4.2.1          PHP version:8.0.2
----------------------------------------------- WORKERS ------------------------------------------------
worker                                          listen                              processes   status
none                                            none                                1           [ok]
PHP Fatal error:  Declaration of Workerman\Mqtt\Protocols\Mqtt::input(string $buffer, Workerman\Connection\ConnectionInterface $connection): int must be compatible with Workerman\Protocols\ProtocolInterface::input($recv_buffer, Workerman\Connection\ConnectionInterface $connection) in F:\mqtt\vendor\workerman\mqtt\src\Protocols\Mqtt.php on line 136

Fatal error: Declaration of Workerman\Mqtt\Protocols\Mqtt::input(string $buffer, Workerman\Connection\ConnectionInterface $connection): int must be compatible with Workerman\Protocols\ProtocolInterface::input($recv_buffer, Workerman\Connection\ConnectionInterface $connection) in F:\mqtt\vendor\workerman\mqtt\src\Protocols\Mqtt.php on line 136
Worker process terminated with ERROR: E_COMPILE_ERROR "Declaration of Workerman\Mqtt\Protocols\Mqtt::input(string $buffer, Workerman\Connection\ConnectionInterface $connection): int must be compatible with Workerman\Protocols\ProtocolInterface::input($recv_buffer, Workerman\Connection\ConnectionInterface $connection) in F:\mqtt\vendor\workerman\mqtt\src\Protocols\Mqtt.php on line 136"
这里粘贴报错

截图报错信息里报错文件相关代码

操作系统及workerman/webman等框架组件具体版本

workerman/mqtt 2.1
workerman/workerman 4.2.1 An asynchronous event driven PHP framework for easily building fast, scalable network ap...

221 1 0
1个回答

JustForFun

mqtt:2.1workerman:4.2.1不兼容的,我看mqttcomposer.json提交记录,其中有一条就是加了workerman的依赖可以是^4.0,应该就是这个导致的。。。如果你的项目只有个这个mqtt,可以试着升级workerman到版本5或者降级mqtt

  • kenshin100 2025-04-30

    您好,请问下 哪两个版本的workerman和mqtt可以对得上?

  • JustForFun 2025-04-30

    另开一个空目录,在空目录里执行composer require workerman/mqtt,再将你的代码也放进去运行就可以了

🔝