【问题标题】:Laravel Echo + Pusher subscription_error on private channel私人频道上的 Laravel Echo + Pusher subscription_error
【发布时间】:2019-06-04 07:52:31
【问题描述】:

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

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

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(data) 输出

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

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

这些路由是由框架本身创建的,从我读到的 Laravel echo 和 Laravel Auth 应该可以很好地协同工作,而不需要太多的摆弄。

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

谁能解释一下这件事?谢谢

【问题讨论】:

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


    【解决方案1】:

    这对我有用

    .env 的默认值为
    BROADCAST_DRIVER=日志

    请改成

    BROADCAST_DRIVER=pusher
    

    我也收到了 ["No callbacks on private-user.1 for pusher:subscription_error"]

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

    【讨论】:

      【解决方案2】:

      您不需要在 broadcasting.php 中将 private- 添加到您的频道,这是自动完成的

      试试这个

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

      【讨论】:

        【解决方案3】:

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

        {"auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
        

        如果出现错误,它将在响应中显示,因为您将调试模式设置为 true。

        【讨论】:

          猜你喜欢
          • 2017-05-12
          • 2020-04-14
          • 2018-01-24
          • 2020-01-15
          • 2018-02-23
          • 2020-11-22
          • 1970-01-01
          • 2021-06-17
          • 2017-11-27
          相关资源
          最近更新 更多