最近我开始研究Laravel 5.3的Laravel-Echo和Pusher组合。我已经成功设置了公共频道并转向了私有频道。但是在尝试授权操作时(包括使用简单的return true语句),我遇到了Laravel从/broadcasting/auth路由返回403的问题。有人能告诉我我做错了什么吗?
App/Providers/BroadcastServiceProvider.php:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;
class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Broadcast::routes();
/*
* Authenticate the user's personal channel...
*/
Broadcast::channel('App.User.*', function ($user, $userId) {
return true;
});
}
}
资源/资产/js/bootstrap.js:
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'My-Key-Here'
});
window.Echo.private('App.User.1')
.notification((notification) => {
console.log(notification.type);
});
我可以在Pusher的调试控制台中看到事件和其负载,但是一旦它到达auth路由就会失败。
Broadcast::routes(['middleware' => ['auth:sanctum']]);
。所以这真的取决于您使用的身份验证类型!但是还是要感谢让我走上了正确的方向 :) 这里有关于该主题的更多信息:https://laravel.com/docs/8.x/broadcasting#authorizing-channels - Arno van Oordt