Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP连接到MQTT代理时出现问题_Php_Mqtt_Mosquitto - Fatal编程技术网

使用PHP连接到MQTT代理时出现问题

使用PHP连接到MQTT代理时出现问题,php,mqtt,mosquitto,Php,Mqtt,Mosquitto,我已经在我的服务器上安装了适用于PHP和mosquitto的mosquitto扩展,它们工作正常。当我将代理与websocket一起使用或使用mqt explorer对其进行测试时,代理工作正常。但当我试图通过PHP连接到MQT服务器时,它给出了这样一条消息。我读了一些主题,有人说这是因为MQT版本冲突 这是一条信息: Fatal error: Uncaught Mosquitto\Exception: A network protocol error occurred when communi

我已经在我的服务器上安装了适用于PHP和mosquitto的mosquitto扩展,它们工作正常。当我将代理与websocket一起使用或使用mqt explorer对其进行测试时,代理工作正常。但当我试图通过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";
}