我已经在centos服务器上安装了蚊子代理。为了进行通信,我安装了Mosquitto PHP库。发布者和订阅者在同一台服务器上工作得很好,但是当我试图从这个centos服务器发布消息,并从本地机器(ubuntu)运行订阅者脚本,然后得到以下错误-
PHP Fatal error: Uncaught Mosquitto\Exception: The client is not currently connected. in /home/sujit/Desktop/php_test.php:12
Stack trace:
#0 /home/sujit/Desktop/php_test.php(12): Mosquitto\Client->subscribe('#', 1)
#1 {main}
thrown in /home/sujit/Desktop/php_test.php on line 12Mosquitto PHP库已经安装在本地机器上。
下面是我正在运行的subscriber.php文件-
<?php
define('BROKER', 'ip address of server');
define('PORT', 1883);
define('CLIENT_ID', "pubclient_" . getmypid());
$client = new Mosquitto\Client(CLIENT_ID);
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect(BROKER, PORT, 60);
$client->subscribe('#', 1); // Subscribe to all messages
$client->loopForever();
function connect($r) {
echo "Received response code {$r}\n";
}
function subscribe() {
echo "Subscribed to a topic\n";
}
function message($message) {
printf("Got a message on topic %s with payload:\n%s\n", $message->topic, $message->payload);
}
function disconnect() {
echo "Disconnected cleanly\n";
}发布于 2019-04-04 20:10:31
将对$client->subscribe('#', 1);的调用移到onConnect回调中。
https://stackoverflow.com/questions/55513102
复制相似问题