代码之家  ›  专栏  ›  技术社区  ›  Araw

客户端无法接收专用通道数据

  •  0
  • Araw  · 技术社区  · 6 年前

    我不能让私人频道使用推送器和Laravel广播。在 routes/channels.php 文件似乎没有一个函数被激发:

    Broadcast::channel('App.User.{id}', function ($user, $id) {
        return (int) $user->id === (int) $id;
    });
    
    
    Broadcast::channel('testevent.{id}', function ($user, $id)
    {
        //This never fires
        dd("ENTERED HERE");
        return TRUE;
    });
    

    BroadcastServiceProvider.php 我有:

    public function boot()
    {
        Broadcast::routes(['middleware' => 'auth:api']);
    
        require base_path('routes/channels.php');
    }
    

    这个 Javascript file 在客户端处理数据(使用echo):

    Echo.private('testevent.1').listen('TestEvent', function(e)
    {
        console.log(e);
    });
    

    使用公共渠道是完美的。但一旦我尝试创建专用通道,数据就不会发送到监听数据的客户机。问题可能是什么?

    感谢您的帮助和指导!

    编辑:

    在pusher Web控制台中,客户端似乎没有订阅“testeevent.1”通道。如果我将此更改为公共频道,订阅将被注册。

    1 回复  |  直到 6 年前
        1
  •  0
  •   JeuneApprenti    6 年前

    在“ Defining authorization callbacks “Laravel广播文档的段落中,您可以看到,在用户能够收听之前,隐私频道需要对用户进行身份验证。

    所以在你的心中 routes/channels.php ,您需要在那里编写身份验证逻辑,例如:

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