使用swoole作为MQTT客户端并接收实现即时消息推送

环境准备 首先需要安装 swoole 可以使用 pecl 进行安装 ,如 pecl install swool, 注意加上版本号

或者使用构建好的 docker 镜像,这里使用构建好的 zacksleo/php:7.1-alpine-fpm-swoole 镜像

使用 compose 安装依赖库

composer require jesusslim/mqttclient

<?php
 
namespace console\controllers;
 
use yii;
use console\components\mqtt\Logger;
use console\components\mqtt\Store;
use yii\console\Controller;
use mqttclient\src\swoole\MqttClient;
use mqttclient\src\subscribe\Topic;
 
class MqttController extends Controller
{
    public function actionClient()
    {
        $r = new MqttClient(getenv('TOKEN_MQTT_HOST'), getenv('TOKEN_MQTT_PORT'), 'push-server-client');
        $r->setAuth(getenv('TOKEN_MQTT_USERNAME'), getenv('TOKEN_MQTT_PASSWORD'));
        $r->setKeepAlive(60);
        $r->setLogger(new Logger());
        $r->setStore(new Store());
        $r->setTopics(
            [
                //消息回执
                new Topic('user-auth/create', function (MqttClient $client, $msg) {
                    //$msg 为获取到的消息体
                }),
                //消息打开
                new Topic('user-auth/delete', function (MqttClient $client, $msg) {
                   //$msg 为获取到的消息体
                })
            ]
        );
        $r->connect();
    }
}
复制代码

阅读原文请访问

www.zxb8.cc/?p=515

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值