0

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

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

在浏览器控制台->网络-> xhr中,我发现请求broadcasting/auth不给我auth:{token}对象,而是返回我的登录页面!!!

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

广播服务提供者.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');
        });
} });

任何帮助将非常感激!

4

2 回答 2

1

对于 Laravel 版本 > 5.3 和 Pusher,您需要在标头请求中添加令牌,您的代码在resources/assets/js/bootstrap.js

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

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

于 2018-05-18T07:21:51.403 回答
0

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

于 2019-01-09T16:48:11.653 回答