Laravel with Pusher 根本不监听事件

Laravel with Pusher simply doenst listen to events

我错过了什么?我做的一切都正确,但是当我发送一个新事件时,我的控制台没有记录任何东西,这是我的代码:

前端

我导入了所需的库:

import Echo from 'laravel-echo';

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

window.pusherEcho = new Echo({
    broadcaster: 'pusher',
    key: 'my-key',
    cluster: 'us2',
    forceTLS: true,
    wsHost: window.location.host,
    wsPort: 8000
});

后端

我的活动class:

<?php

namespace App\Events;

// ... imports

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

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    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('received-notification');
    }
}

我注册了一个路由只是为了测试它:

Route::get('/event', function() {
    event(new App\Events\ReceivedNotification('Hello world'));

    return 'event sent';
});

前端

我正在使用 Laravel Echo 收听某些页面中的事件:

pusherEcho.channel('received-notification')
    .listen('ReceivedNotification', (e) => {
        console.log(e);
});

当我去路线 /event 时,它永远不会触发上面的 console.log,这意味着有些地方出了问题,是的,我的 .env 文件一切正常,我已经检查过了:

BROADCAST_DRIVER=pusher
. 
.
.
PUSHER_APP_ID=***
PUSHER_APP_KEY=***
PUSHER_APP_SECRET=***
PUSHER_APP_CLUSTER=us2

我们可以看到您正在 App\Events\ReceivedNotification 中创建私人频道并在前端收听 public 频道。 因此,将您的前端更改为以下代码:

pusherEcho.private('received-notification')
    .listen('ReceivedNotification', (e) => {
        console.log(e);
});

同时在 app\Providers\EventServiceProvider 中注册您的事件和监听器,例如:

protected $listen = [
   'App\Events\ReceivedNotification' => [
            'App\Listeners\ReceivedNotificationListener'
        ],
]