客户端不会接收专用通道数据



我无法使用Pusher和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);
});

使用公共频道效果很好。但是,一旦我尝试创建专用通道,数据就不会发送到侦听数据的客户端。问题可能是什么?

感谢您的任何帮助和指导!

编辑:

在推送器 Web 控制台中,客户端似乎没有订阅"testevent.1"频道。如果我将其更改为公共频道,订阅就会注册。

在 Laravel 广播文档的"定义授权回调"段落中,您可以看到专用频道需要对用户进行身份验证,然后他们才能收听他们。

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

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

最新更新