即使用户通过身份验证,Laravel Echo也不会订阅专用频道 [英] laravel echo wont subscribe to private channel even the user is authenticated

查看:0
本文介绍了即使用户通过身份验证,Laravel Echo也不会订阅专用频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

  • 拉威尔:8.54
  • 拉威尔·圣殿:2.11
  • Pusher PHP服务器:7.0
  • NuxtJs:2.15.7
  • Pusher-js:7.0.4
  • LAVELEL-ECHO:1.11.3

事件类

    <?php

namespace AppEvents;

use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcastNow;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;

class RaffleEntriesCreated implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $userid;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($userid)
    {
        $this->userid = $userid;
    }

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'private-raffle-entry';
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return IlluminateBroadcastingChannel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('RaffleEntry.'.$this->userid);
    }
}

我的频道文件

Broadcast::channel('RaffleEntry.{id}',  function ($user, $id) {
    return (int) $user->id === (int) $id;
});

启用nuxt插件

       import Echo from 'laravel-echo';
        window.Pusher = require('pusher-js');
        export default ({ app }, inject) => {
          const echo = new Echo({
              broadcaster: 'pusher',
              key: "mykey",
              authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
              encrypted: true,
              forceTLS: true,
              cluster: 'ap2',
              authorizer: (channel, options) => {
                return {
                    authorize: (socketId, callback) => {
                        app.$axios.$post('/api/broadcasting/auth', {
                            socket_id: socketId,
                            channel_name: channel.name
                        })
                        .then(response => {
                            callback(false, response.data);
                            console.log('from oklugib ');
                        })
                        .catch(error => {
                            callback(true, error);
                        });
                    }
                };
              }
          });
          inject(
    
    'echo', echo)
    }

客户端代码

 this.$echo.private(`RaffleEntry.${this.$auth.user.id}`)
        .listen('.private-raffle-entry', (e) => {
            console.log(e);
        });

我可以看到广播消息进入Pusher仪表板,也可以看到客户端连接,用户可以完美地进行身份验证,问题是在身份验证后没有订阅。请在订阅前查看这些用户身份验证请求的屏幕截图。你知道我该怎么解决这个问题吗?

推荐答案

好的解决了这个问题,在我的nuxt插件文件中,您只需返回responsevar Notrespose.data

import Echo from 'laravel-echo';
        window.Pusher = require('pusher-js');
        export default ({ app }, inject) => {
          const echo = new Echo({
              broadcaster: 'pusher',
              key: "mykey",
              authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
              encrypted: true,
              forceTLS: true,
              cluster: 'ap2',
              authorizer: (channel, options) => {
                return {
                    authorize: (socketId, callback) => {
                        app.$axios.$post('/api/broadcasting/auth', {
                            socket_id: socketId,
                            channel_name: channel.name
                        })
                        .then(response => {
                            callback(false, response);
                     
                        })
                        .catch(error => {
                            callback(true, error);
                        });
                    }
                };
              }
          });
          inject(
    
    'echo', echo)
    }

这篇关于即使用户通过身份验证,Laravel Echo也不会订阅专用频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆