大佬帮看看mqtt的实例报错吧

864328615

问题描述

还没有链接 就报错了

程序代码

 public function onWorkerStart()
    {
        var_dump(getenv('MQTT_HOST') . ':' . getenv('MQTT_PORT'));
        var_dump(getenv('MQTT_USER') . ':' . getenv('MQTT_PASSWORD'));
        $mqtt = new Client(getenv('MQTT_HOST') . ':' . getenv('MQTT_PORT'), [
            'username' => getenv('MQTT_USER'),
            'password' => getenv('MQTT_PASSWORD'),
        ]);
    }

报错信息

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 /Users/zhengjiefeng/yujianshanhai/api-device-dept/vendor/workerman/mqtt/src/Protocols/Mqtt.php on line 134

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

macos
php8.3

413 2 0
2个回答

walkor 打赏

提问时把workerman 和 mqtt版本发下。
composer info 能看到版本信息

  • 暂无评论
软饭工程师

workerman/mqtt 2.0
workerman/webman-framework 1.5.19 High performance HTTP Service Framework.
用的docker安装的,emqx/emqx:5.8.2 镜像启动的MQTT服务器
示例代码

 // config/process.php
    'mqttclient' => [
        'handler' => process\MqttClient::class,
    ],
//// process/MqttClient.php

<?php

namespace process;

class MqttClient
{
    public function onWorkerStart()
    {
        $mqtt = new \Workerman\Mqtt\Client('mqtt://broker.emqx.io:1883', array(
            'debug' => true,
            "username"=>"rw", "password"=>"readwrite"
        ));
        $mqtt->onConnect = function($mqtt) {
            $mqtt->subscribe('workerman');
        };
        $mqtt->onMessage = function($topic, $content){
            echo "topic:$topic content:$content\n";
        };
        \Workerman\Timer::add(2, function() use ($mqtt) {
            $mqtt->publish('workerman', 'hello workerman mqtt');
        });
        $mqtt->connect();
    }
}

出现错误信息

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 /Users/xx/webman-test1/vendor/workerman/mqtt/src/Protocols/Mqtt.php on line 134
Worker[73591] 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 /Users/xx/webman-test1/vendor/workerman/mqtt/src/Protocols/Mqtt.php on line 134"
  • walkor 27天前

    现在最新版本是2.1,升级到最新

  • 软饭工程师 26天前

    可以了

  • 软饭工程师 25天前

    我想直接发送主题消息给MQTT,应该怎么处理,不开自定义进程,不用处理订阅主题消息

        public function test30()
        {
    
            try {
                $mqtt = new \Workerman\Mqtt\Client('mqtt://broker.emqx.io:1883', array(
                    'debug' => true,
                    "username"=>"rw", "password"=>"readwrite"
                ));
                $mqtt->connect();
                $data = [
                    'topic' => 'workerman',
                    'content' => json_encode(['yl' => 01]),
                ];
                $mqtt->publish($data['topic'], $data['content']);
                $mqtt->close();
                return $this->success();
            } catch (\Exception $e) {
                echo "Error: " . $e->getMessage();
            }
        }

    出现报错

    -> Try to connect to broker.emqx.io:1883/, protocol: Workerman\Mqtt\Protocols\Mqtt
    -- Error: No connection to broker
    Mqtt client: No connection to broker
    -> Connection->close() called
    -- Connection closed
    
  • chaz6chez 23天前

    client的操作是异步的,publish的时候并不一定connected了,所以不能用阻塞的思想写;另外也不建议短链接client,建议持久化;

×
🔝