Laravel эхо не отвечает вообще - PullRequest
0 голосов
/ 28 января 2020

У меня есть bootstrap модал. Когда модал открыт, я хочу добавить пользователя в «очередь». Когда модальное окно закрыто, я хочу удалить их из указанной очереди. Они также должны быть удалены из очереди, если они покидают страницу et c.

Вот что я сделал.

Настроен .env:

BROADCAST_DRIVER=pusher

PUSHER_APP_ID=(i put my app id here)
PUSHER_APP_KEY=(i put my app key here)
PUSHER_APP_SECRET=(i put my app secret here)
PUSHER_APP_CLUSTER=us2

Я раскомментировал App\Providers\BroadcastServiceProvider::class, в config.app.

Добавлен канал в routes/channel:

Broadcast::channel('queue', function (\App\User $user) {
    return $user->toArray();
});

Созданы мои классы событий:

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

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function broadcastOn()
    {
        return new PresenceChannel('queue');
    }
}

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

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function broadcastOn()
    {
        return new PresenceChannel('queue');
    }
}

Добавлено мои сценарии (я использую laravel livewire):

<script>
    window.livewire.on('show-modal', () => {
        $('#queue-modal').modal('show');
        console.log('show-modal'); // called

        Echo.join('queue')
            .joining((user) => {
                window.livewire.call('joining-queue');
                console.log('joining: ' + user.name); // not called
            })
            .leaving((user) => {
                window.livewire.call('leaving-queue');
                console.log('leaving: ' + user.name); // not called
            })
            .listen('QueueJoined', (e) => {
                console.log('QueueJoined: ' + e.user.name); // not called
            })
            .listen('QueueLeft', (e) => {
                console.log('QueueLeft: ' + e.user.name); // not called
            });
    });
    window.livewire.on('hide-modal', () => {
        $('#queue-modal').modal('hide');
        console.log('hide-modal'); // called

        Echo.leave('queue');
    });
</script>

Я прокомментировал, где выполняются вызовы.

Это мой класс компонента livewire:

class Queue extends Component
{
    public $message;

    public function render()
    {
        return view('livewire.queue');
    }

    public function showModal()
    {
        $this->message = 'Joining queue...';
        $this->emit('show-modal');
    }

    public function hideModal()
    {
        $this->message = 'Leaving queue...';
        $this->emit('hide-modal');
    }

    public function joiningQueue()
    {
        $this->message = 'Queue joined! Finding match...';
        // add to queue table
        broadcast(new QueueJoined(Auth::user()));
    }

    public function leavingQueue()
    {
        $this->message = 'Queue left.';
        // remove from queue table
        broadcast(new QueueLeft(Auth::user()));
    }
}

У меня тоже сейчас работает php artisan queue:listen.

Ничего не работает в отношении трансляции. Pusher даже показывает 0 соединений. Я также не получаю никаких ошибок в журнале ошибок Laravel или Chrome консоли.

Не уверен, что я делаю здесь неправильно, поэтому любая помощь будет оценена.

...