0

我是 Pusher 和 Laravel Echo 的新手,并逐步学习它。我正在尝试在我的新项目中实现它,但不知何故,我无法理解这个推送器库如何与 Laravel Echo 一起使用。

我正在做的是,在管理员注册时,我只想检查我所做的是否正确。我只是想在我的推送器仪表板帐户的调试控制台上查看我生成的事件的输出。

我创建了一个AdministratorGenerated包含以下内容的事件:

class AdministratorGenerated implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public $user;

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

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

        // To send it as public channel to pusher
        return['administrator'];
    }
}

在 bootstrap.js 文件中,我有:

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'my-pusher-key',
    cluster: 'ap1',
    encrypted: true
});

window.Echo.channel('administrator')
    .listen('AdministratorGenerated', (e) => {
        console.log(e);
    });

成功注册表单后,我正在触发一个事件

event(new AdministratorGenerated($registeredUser));

此事件将触发欢迎电子邮件并设置默认帐户。

但是,当我进入 Pusher 仪表板中的调试控制台时,我看到的只有以下内容:

在此处输入图像描述


编辑1:

这是.env文件:

APP_ENV=local
APP_KEY=base64:toktfSBbJM0vbylxhT/zHXOi7zVga9jsliB/mtE96HY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=larammerce_v1
DB_DATABASE_TESTING=larammerce_v1_testing
DB_USERNAME=root
DB_PASSWORD=root

BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=database

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

# All of the below values are correct
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null

# All of the below values are correct
PUSHER_APP_ID=id
PUSHER_KEY=key
PUSHER_SECRET=secret

这是broadcasting.php配置文件

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Broadcaster
    |--------------------------------------------------------------------------
    |
    | This option controls the default broadcaster that will be used by the
    | framework when an event needs to be broadcast. You may set this to
    | any of the connections defined in the "connections" array below.
    |
    | Supported: "pusher", "redis", "log", "null"
    |
    */

    'default' => env('BROADCAST_DRIVER', 'pusher'),

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => 'ap1',
                'encrypted' => true
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

];

我在做什么错误?有人可以帮我解决这个问题吗?

提前致谢。

4

1 回答 1

5

您的活动似乎还没有正确到达 Pusher。可能是您的广播配置配置错误。

首先检查config/broadcasting.php并定义您的 Pusher 连接的集群:

'options' => [
    'cluster' => 'your_cluster',
]

设置默认值:BROADCAST_DRIVER=pusher在您的.env文件中。

再次尝试触发您的AdministratorGenerated事件,看看是否在 Pusher 调试控制台中登录。

您还使用 PrivateChannel 所以window.Echo.channel('administrator')更可能必须是window.Echo.private('administrator'). 再次运行 gulp。

于 2016-09-03T13:06:10.420 回答