workerman/mqtt 怎么使用

sorshion

问题描述

目前的场景是手机遥控器的场景,手机遥控器发送指令到接口,接口接收到后mqtt 发布到emqx,

为此你搜索到了哪些方案及不适用的原因

因为接口使用的是webman,我看了几个帖子,其中有说到做一个自定义进程
代码如何

class MqttTask
{
    public static function onWorkerStart()
    {
        $mqtt = new \Workerman\Mqtt\Client('mqtt://xxxx:1883', array(
            //'debug' => true,
            "username"=>"xxx",
            "password"=>"xxx",
            "client_id"=>"xxx"
        ));
        $mqtt->onConnect = function($mqtt) {
            $mqtt->subscribe('test');
        };
        $mqtt->onMessage = function($topic, $content) {
            echo "topic:$topic content:$content\n";
        };
        $mqtt->connect();
    }
}

主要是这里的onMessage 又是如何接到到topic呢,我就不知道该如何处理呢,请教大家,如何做

466 1 0
1个回答

Gin
class MqttTask
{
    public function onWorkerStart()
    {
        global $mqtt;
        $mqtt = new \Workerman\Mqtt\Client('mqtt://xxxx:1883', array(
            //'debug' => true,
            "username"=>"xxx",
            "password"=>"xxx",
            "client_id"=>"xxx"
        ));
        $mqtt->onConnect = function($mqtt) {
            //这里是订阅的设备端的信息
            $mqtt->subscribe('test');
        };
        $mqtt->onMessage = function($topic, $content) {
            //设备端回复的 主题信息
            echo "topic:$topic content:$content\n";
        };
        $mqtt->connect();
    }
    public function onMessage($connection, $data)
    {

        global $mqtt;
        //通过data中的信息  动态发布
        $mqtt->publish($data['topic'], $data['order']);

        $connection->close($data);

    }
}
  • 暂无评论
×
🔝