使用PHP连接到MQTT代理的问题

问题描述

我在我的服务器上安装了适用于PHP和mosquitto的mosquitto扩展程序,它们可以正常工作。当我将其与websocket一起使用或与mqt资源管理器一起测试时,代理可以正常工作。但是当我尝试通过PHP连接到MQT服务器时,它会显示此消息。我读过很少的主题,有人说这是因为mqt版本冲突。

这是消息:

Fatal error: Uncaught Mosquitto\Exception: A network protocol error occurred when communicating with the broker. in /var/www/html/mqt/sub.PHP:17 Stack trace: #0 /var/www/html/mqt/sub.PHP(17): Mosquitto\Client->loop() #1 {main} thrown in /var/www/html/mqt/sub.PHP on line 17

这是代码

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

$client = new Mosquitto\Client();
$client->setCredentials('Zeperto','aliAKBAR2210');
$client->onConnect('connect');
$client->ondisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost",1883,60);
$client->subscribe('myTopic',1);


while (true) {
        $client->loop();
        sleep(2);
}

$client->disconnect();
unset($client);

function connect($r) {
        echo "I got code {$r}\n";
}

function subscribe() {
        echo "Subscribed to a topic\n";
}

function message($message) {
        printf("\nGot a message on topic %s with payload:%s",$message->topic,$message->payload);
}

function disconnect() {
        echo "disconnected cleanly\n";
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)