如何使用libmosquitto测试sub.php和pub.php [英] How can test sub.php and pub.php using libmosquitto
问题描述
我在覆盆子中安装mosquitto并测试mosquitto_sub& mosquitto_pub现在正在运行我需要创建两个文件sub.php& pub.php
,我的等式必须放这些文件我的意思是什么路径正确,如果需要把这些文件放在这个路径/ var / www / html,怎么样可以
运行这些文件没有错误
sub.php
I install mosquitto in raspberry and test mosquitto_sub & mosquitto_pub is Running now I need create two files sub.php & pub.php
, my equation where must put these files I mean what path correct, also if need put these file in this path /var/www/html , how can
run these file without error
sub.php
<?php
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 60);
$client->subscribe('/#', 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";
}
pub.php
pub.php
<?php
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onPublish('publish');
$client->connect("localhost", 1883, 5);
while (true) {
try{
$client->loop();
$mid = $client->publish('/mqtt', "Hello from PHP");
$client->loop();
}catch(Mosquitto\Exception $e){
return;
}
sleep(2);
}
$client->disconnect();
unset($client);
function connect($r) {
echo "I got code {$r}\n";
}
function publish() {
global $client;
echo "Mesage published\n";
$client->disconnect();
}
function disconnect() {
echo "Disconnected cleanly\n";
}
我尝试过:
我尝试将sub.php和pub.php放在路径/ var / www / html中并写入broswer
210.201 .4.111 / sub.php也写210.201.4.111/pub.php
但看不到任何数据
What I have tried:
I try put sub.php and pub.php in the path /var/www/html and write in broswer
210.201.4.111/sub.php also write 210.201.4.111/pub.php
but not see any data
推荐答案
client = new Mosquitto \Client();
client = new Mosquitto\Client();
client-> onConnect(' connect');
client->onConnect('connect');
client-> onDisconnect(' disconnect');
client->onDisconnect('disconnect');
这篇关于如何使用libmosquitto测试sub.php和pub.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!