我已经构建了一个Laravel应用,试图通过Pusher.com实现Web套接字(这是第一次)。
虽然我的公共频道订阅正常,但我仍在努力使私人频道正常工作。
根据laravel文档,您需要在我拥有的a
配置文件中取消注释App\Providers\BroadcastServiceProvider::class
。
我的channels.php具有以下规则
app.php
要使Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('private-queue.business.{business}', function ($user, Business $business) {
// @todo: add real authentication
return true;
});
端点正常工作,还需要添加其他内容吗?
答案 0 :(得分:1)
您可以尝试: 试试这个:
取消注释config / app.php中的App \ Providers \ BroadcastServiceProvider :: class 使用php artisan config:cache 使用php artisan route:cache 使用php artisan route:list检查新的路由广播/身份验证
答案 1 :(得分:1)
从Laravel 7.x开始,广播端点为broadcasting/auth
,而不是pusher/auth
。
我需要像这样更新我的JS,以便能够定义自定义身份验证端点:
const pusher = new Pusher('{{ env('PUSHER_APP_KEY') }}', {
cluster: '{{ env('PUSHER_APP_CLUSTER') }}',
authEndpoint: '/broadcasting/auth',
auth: {
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
}
}
});
您将需要添加CSRF-TOKEN,否则将出现Page Expired错误。