Laravel Echo + Pusher:请求到broadcasting/auth returns 登录页面

Laravel Echo + Pusher: request to broadcasting/auth returns login page

我将我的应用程序从 5.2 更新到 5.3 以使用 Pusher 广播通知,但是当 Pusher 尝试通过 broadcasting/auth 验证当前登录用户时,我收到一个错误:

Pusher : No callbacks on private-App.Models.Client.9 for pusher:subscription_error

在浏览器console->network->xhr中,我发现对broadcasting/auth的请求没有给我auth:{token}对象,而是返回我的登录页面!!!

我认为是中间件的问题,但我找不到。

BroadcastServiceProvider.php:

public function boot()
{
    // Broadcast::routes();
    Broadcast::routes(['middleware' => ['auth:client']]);

    /*
     * Authenticate the user's personal channel...
     */
    Broadcast::channel('App.Models.Client.*', function ($user, $userId) {
        return true;
    });

}

app.js: 导入 pusher-js & Laravel Echo

$(document).ready(function() {
// check if there's a logged in user
if(Laravel.clientId) {
    window.Echo.private(`App.Models.Client.${Laravel.clientId}`)
        .notification((notification) => {
            console.log(notification);
            addNotifications([notification], '#notifications');
        });
} });

任何帮助将不胜感激!

使用 Laravel 版本 > 5.3 & Pusher,您需要在 header 请求中添加令牌,您的代码在 resources/assets/js/bootstrap.js

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your key',
    cluster: 'your cluster',
    encrypted: true,
    auth: {
        headers: {
            Authorization: 'Bearer ' + YourTokenLogin
        },
    },
});

对我有用,希望对你有帮助。

纠结了半天,终于找到问题的原因了。我正在为 Authentication/Authorization 使用一个名为 cartalyst/sentinel 的包,它很棒,除了一旦安装它将完全取代 Laravel 默认的身份验证行为。所以任何对 broadcasting/auth 的请求实际上都被拒绝了。