回声通道未监听事件

时间:2019-08-17 09:59:54

标签: laravel websocket

不听课TravelPlannerEvent 顺便说一下,我使用nwidart / laravel-modules包

我试图收听TravelPlannerEvent和Modules \ Transfer \ Events \ TravelPlannerEvent,但它不起作用。

Echo.private('transfer').listen('TravelPlannerEvent', (e) => {
  alert(e);
}).listen('Modules\\Transfer\\Events\\TravelPlannerEvent', (e) => {
  alert(e);
});

TravelPlannerEvent类

<?php

namespace Modules\Transfer\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Modules\Transfer\Entities\Transfer;

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

    private $transfer;

    /**
     * TravelPlannerEvent constructor.
     * @param Transfer $transfer
     */
    public function __construct(Transfer $transfer)
    {
        $this->transfer = $transfer;
    }

    /**
     * @return Channel
     */
    public function broadcastOn()
    {
        return new PrivateChannel('transfer');
    }

    /**
     * @return array
     */
    public function broadcastWith()
    {
        $user = $this->transfer->driver ? $this->transfer->driver->user : $this->transfer->driver_team->driver->user;
        return [
            'notification' => [
                'type' => 'transfer',
                'transfers_id' => $this->transfer->id,
                'photo_link' => $user->profile_photo,
                'user' => $user->full_name,
                'created_at' => $this->transfer->created_at->format(showDateFormat() . ' H:m:s'),
            ]
        ];
    }
}

这是websocket服务的日志

{"channel":"private-transfer","event":"Modules\\Transfer\\Events\\TravelPlannerEvent","data":"{\"notification\":{\"type\":\
"transfer\",\"transfers_id\":58,\"photo_link\":\"http:\\\/\\\/127.0.0.1:8000\\\/storage\\\/photos\\\/users\\\/8PXIOvo2HKOezy4tT5zbUy5YIyI3uFwhaxtrmvN4.jpeg\",\"user\":\"Ion
Podolean\",\"created_at\":\"06\\\/08\\\/2019 07:08:07\"}}"}

0 个答案:

没有答案