【问题标题】:Pusher: JSON returned empty stringPusher:JSON 返回空字符串
【发布时间】:2020-07-11 16:42:44
【问题描述】:

我正在尝试使用 Pusher 和 Laravel-Echo 在 Laravel 中制作实时通知系统。所以在 Vue 文件中,当我连接到私有通道时,从 auth 端点返回的 JSON 是空字符串。

这是我的代码。

config/auth.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'teacher' => [
            'driver' => 'session',
            'provider' => 'teacher',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'teacher' => [
            'driver' => 'eloquent',
            'model' => App\Teacher::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

resources/js/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,
    encrypted: true,    
});

Pusher.logToConsole = true;

Vue 文件,在挂载函数中连接私有通道

mounted(){
        window.Echo.private('homeworknotif')
        .listen('HomeworkAdded', e => {
          console.log(e);
        })
      },

家庭作业添加事件文件

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class HomeworkAdded implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $message;
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('homeworknotif');
    }
}

routes/channels.php

Broadcast::channel('homeworknotif', function($user, $id){
    return true;
});

在控制器中广播

public function eventer(){
        broadcast(new HomeworkAdded('hello world'));
    }

当我将window.Echo.private更改为window.Echo.channel时,没有错误,控制台显示日志,我发送了数据,但数据仅在日志中可见。

【问题讨论】:

  • 听起来您的客户可能无法订阅私人频道。您是否看到订阅有任何错误,或在调试控制台中成功订阅了频道? support.pusher.com/hc/en-us/articles/…
  • @doydoy 在调试控制台中每次刷新页面后,我都会看到连接。
  • 您也看到订阅了吗?连接表明您的客户端可以与 Pusher 通信,但它仍然需要订阅通道(这将是在进行身份验证时)。您也应该能够在仪表板中看到错误日志,这可能有助于识别问题。
  • @doydoy 没有错误。我会尝试其他方法。感谢您的评论。现在我对 pusher 有了更多的了解 :)

标签: laravel vue.js pusher


【解决方案1】:

我不敢相信,但我没有在 .env 文件中设置我的 BROADCAST_DRIVER,它是日志而不是推送器。

【讨论】:

    猜你喜欢
    • 2020-01-27
    • 2015-05-25
    • 2011-05-09
    • 1970-01-01
    • 2012-07-20
    • 2011-12-15
    • 2020-11-23
    • 2015-12-21
    • 2013-09-02
    相关资源
    最近更新 更多