ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # 举个栗子 底层库是基于回调和事件驱动的操作。因此,您必须经常调用客户端的`loop()`方法,以允许库处理其队列中的消息。你也可以使用`loopforever()`,这样就不必频繁调用方法。还有,您应该使用回调函数来确保您只在客户端连接后再尝试发布,等等。例如,以下是如何正确发布qos = 2消息的例子: ~~~ <?php $c = new Mosquitto\Client; $c->onConnect(function() use ($c) { $c->publish('mgdm/test', 'Hello', 2); $c->disconnect(); }); $c->connect('test.mosquitto.org'); // Loop around to permit the library to do its work // This function will call the callback defined in `onConnect()` // and disconnect cleanly when the message has been sent $c->loopForever(); echo "Finished\n"; ~~~ * * * * *