账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Coroutine\Redis 订阅模式 调用退订无效果
    44
    0

    $redis = new SwooleCoroutineRedis();
    $redis->connect('127.0.0.1', 6379);
    if ($redis->subscribe(['channel1', 'channel2', 'channel3'])) // 或者使用psubscribe
    {

    while ($msg = $redis->recv()) {
        // msg是一个数组, 包含以下信息
        // $type # 返回值的类型:显示订阅成功
        // $name # 订阅的频道名字 或 来源频道名字
        // $info  # 目前已订阅的频道数量 或 信息内容
        list($type, $name, $info) = $msg;
        if ($type == 'subscribe') // 或psubscribe
        {
            // 频道订阅成功消息,订阅几个频道就有几条
        }
        else if ($type == 'unsubscribe' && $info == 0) // 或punsubscribe
        {
            break; // 收到取消订阅消息,并且剩余订阅的频道数为0,不再接收,结束循环
        }
        else if ($type == 'message') // 若为psubscribe,此处为pmessage
        {
            // 打印来源频道名字
            var_dump($name);
            // 打印消息
            var_dump($info);
            // 处理消息
            // balabalaba....
            if ($need_unsubscribe) // 某个情况下需要退订
            {
                $redis->unsubscribe(); // 继续recv等待退订完成
            }
        }
    }

    }

    调用 $redis->unsubscribe()时,类型为unsubscribe ,info为0 时,执行break或$redis->close();没有退出while循环

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 莫晓渡 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部