Laravel Echo + Pusher subscription_error在私人频道上

时间:2019-01-09 13:58:25

标签: php laravel pusher laravel-echo pusher-js

在使用私人频道,搜索了两天并找到了nada时,我无法设法使echo和pusher正常工作。

似乎正在发生的身份验证存在某种问题(我正在使用Laravel基本Auth),因为我可以订阅公共频道

routes / channels.php

Broadcast::channel('private-ci-received-{userId}', function ($user, $userId) {
    return (int) $user->id === (int) $userId;
});

Broadcast::channel('private-ci-received-{toUserId}', function ($currentUser, $toUserId) {
    return true;
});

bootstrap.js

import Echo from 'laravel-echo'

 window.Pusher = require('pusher-js');

 window.Echo = new Echo({

     broadcaster: 'pusher',
     key: process.env.MIX_PUSHER_APP_KEY,
     cluster: process.env.MIX_PUSHER_APP_CLUSTER,
     logToConsole: true,
     encrypted: true,
 });

default.blade.php(主布局)

        Pusher.logToConsole = true;
        Echo.logToConsole = true;

        Echo.private('ci-received-{{ Auth::user()->id}}')
            .listen('.CIReceived', (e) => {
                console.log(e);
            });

控制台上显示的内容是:

Pusher : No callbacks on private-ci-received-1 for pusher:subscription_error

这是一个非常普通的错误,出于调试目的,我尝试使用Pusher(而不是laravel echo)将错误绑定

var pusher = new Pusher('MYSECRETAPPKEYHERE', {
            cluster: 'us2',
            forceTLS: true
        });

        var channel = pusher.subscribe('private-ci-received-1');
        channel.bind('pusher:subscription_error', function(data) {
            console.log(data);
        });

console.log(数据)输出

JSON returned from webapp was invalid, yet status code was 200

默认的authEndPoint是/ broadcasting / auth IIRC,我认为它希望返回JSON,但是会从我的页面返回HTML代码。

那些路由是由框架本身创建的,从我读过的内容来看,Laravel echo和Laravel Auth应该可以很好地协同工作,而不会产生任何麻烦。

我的.env文件是正确的,我正在使用pusher作为广播驱动程序,并且BroadcastServiceProvider正确未注释。

有人可以阐明这件事吗?谢谢

2 个答案:

答案 0 :(得分:0)

APP_DEBUG=true文件中设置.env,并在浏览器开发人员工具的网络部分中检查身份验证路由的HTTP响应。如果身份验证成功,则您的laravel应用应使用以下JSON进行响应:

{"auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

如果发生错误,由于将调试模式设置为true,它将在响应中向您显示。

答案 1 :(得分:0)

这对我有用

.env的默认值为
BROADCAST_DRIVER = log

请更改为

BROADCAST_DRIVER=pusher

我还收到了[“ pusher:subscription_error上没有对private-user.1的回调”]

进行上述更改后,对我来说效果很好

相关问题