我正处于MQTT-PHP之旅的初级阶段。我在互联网上找到的关于这个主题的教程很少。我按照说明安装了必要的包。我可以执行示例代码并获得预期的结果。但发布者客户端方案对我不起作用。
发布商代码:
<?php
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect('localhost', 1883, 60);
$client->loop();
$message = "Message sent by natarajan at " . date("Y-m-d H:i:s");
$mid = $client->publish('/hello', $message, 2, True);
echo "Sent message ID: {$mid}\n";
$client->$client->loop(2,10);
function connect($r) {
echo "I got code {$r}\n";
}
function subscribe() {
echo "Subscribed to a topic\n";
}
function message($message) {
printf("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);
}
function disconnect() {
echo "Disconnected cleanly\n";
}
?>
出版商O / P:
I got code 0 Sent message ID: 1
订户代码:
<?php
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 60);
$client->subscribe('/#', 2);
$client->loop(2,10);
function connect($r) {
echo "I got code {$r}\n";
}
function subscribe() {
echo "Subscribed to a topic\n";
}
function message($message) {
printf("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);
}
function disconnect() {
echo "Disconnected cleanly\n";
}
订阅者O / p:
I got code 0 Subscribed to a topic
但我既没有问题也没有信息。提前谢谢。
答案 0 :(得分:0)
您运行的mqtt经纪人的版本和风格是什么?您可以从命令行成功发布和订阅主题/消息吗?不确定php版本处于什么状态,但它看起来并没有被积极维护(有人在这里纠正我!)