thinkphp中使用Mosquitto报错app\controller\Mosquitto\Client

报错信息:
image.png

<?php

namespace app\controller;
//error_reporting(0); 


use app\BaseController;
use think\facade\Db;

//simps
//  use Simps\MQTT\Client;
//  use Simps\MQTT\Config\ClientConfig;
//  use function Swoole\Coroutine\run;

//Mosquitto
use Mosquitto\Client;

class Index extends BaseController
{
    
    public function t9(){
        
                    
                    ##此处填写阿里云帐号 AccessKey
            $accessKey = '1';
            ##此处填写阿里云帐号 SecretKey
            $secretKey = '2';
            ## 接入点地址,购买实例后从控制台获取
            $endPoint = 'pmqtt.aliyuncs.com';
            ##实例 ID,购买后从控制台获取
            $instanceId = '7mz2jl6k40e';
            ## MQTT Topic,其中第一级 Topic 需要在 MQTT 控制台提前申请
            $topic = 'test1_mqtt_topic';
            ## MQTT 客户端ID 前缀, GroupID,需要在 MQTT 控制台申请
            $groupId = 'GID_tt11';
            ## MQTT 客户端ID 后缀,DeviceId,业务方自由指定,需要保证全局唯一,禁止 2 个客户端连接使用同一个 ID
            $deviceId = '1388614';
            $qos = 0;
            $port = 1883;
            $keepalive = 90;
            $cleanSession = true;
            $clientId = $groupId . '@@@' . $deviceId;
            echo $clientId . "\n";
            
            $mid = 0;
            ## 初始化客户端,需要设置 clientId 和 CleanSession 参数,参考官网文档规范
            $mqttClient = new Mosquitto\Client($clientId, $cleanSession);
            
            
            
            ## 设置鉴权参数,参考 MQTT 客户端鉴权代码计算 username 和 password
            $username = 'Signature|' . $accessKey . '|' . $instanceId;
            $sigStr = hash_hmac("sha1", $clientId, $secretKey, true);
            $password = base64_encode($sigStr);
            echo "UserName:" . $username . "  Password:" . $password . "\n";
            $mqttClient->setCredentials($username, $password);
            
            ## 设置连接成功回调
            $mqttClient->onConnect(function ($rc, $message) use ($mqttClient, &$mid, $topic, $qos) {
                echo "Connnect to Server Code is " . $rc . " message is " . $message . "\n";
                $mqttClient->subscribe($topic, $qos);
            });
            
            
            ## 设置订阅成功回调
            $mqttClient->onSubscribe(function () use ($mqttClient, $topic, $qos) {
                $mqttClient->publish($topic, "Hello MQTT PHP Demo", $qos);
            });
            
            ## 设置发送成功回调
            $mqttClient->onPublish(function ($publishedId) use ($mqttClient, $mid) {
                echo "publish message success " . $mid . "\n";
            });
            
            
            ## 设置消息接收回调
            $mqttClient->onMessage(function ($message) {
                echo "Receive Message From mqtt, topic is " . $message->topic . "  qos is " . $message->qos . "  messageId is " . $message->mid . "  payload is " . $message->payload . "\n";
            
            });
            $mqttClient->connect($endPoint, $port, $keepalive);
            
            
            $mqttClient->loopForever();
            
            echo "Finished";
                    
    }
    }
阅读 2.6k
1 个回答

你在前面 use 了 use Mosquitto\Client; 这里直接写 new Client 即可。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题