dpl74687 2017-09-09 10:49
浏览 69
已采纳

Laravel广播事件没有解雇

I am trying to broadcast an event every time a new market snapshot is created. I followed the laravel tutorial here.

But it seems I am missing something since even the log is not being created:

    namespace App\Events;

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

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

        /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }

        /**
         * Get the channels the event should broadcast on.
         *
         * @return Channel|array
         */
        public function broadcastOn()
        {
            \Log::info('event broadcasted');

            return new Channel('snapshot');
        }
    }

I even registered the event in EventServiceProvider (not mentioned on the laravel link)

protected $listen = [
    'App\Events\NewMarketSnapshot' => [
      'App\Listeners\NewMarketSnapshotListener',
    ],
];

The handler is being called but the event is not being broadcasted.

    namespace App\Listeners;

    use App\Events\NewMarketSnapshot;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use \Log;

    class NewMarketSnapshotListener
    {
        /**
         * Create the event listener.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }

        /**
         * Handle the event.
         *
         * @param  NewMarketSnapshot  $event
         * @return void
         */
        public function handle(NewMarketSnapshot $event)
        {
            Log::info('handler called.');
        }
    }

Testing with the simple route:

Route::get('/test', function(){
    $snapshot = ['USD'=>100];
    return event(new App\Events\NewMarketSnapshot($snapshot));
});

Lines in my .env file

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
  • 写回答

1条回答 默认 最新

  • doutan2111 2017-09-09 11:27
    关注

    I note that you are using Redis as both the queue and broadcast driver. My experience has been that using it for both breaks the broadcasting (basically it sends the wrong command to Redis on the broadcast event).

    If you switch the queue to a different driver or use separate Redis instances it should hopefully work OK.

    EDIT: I believe under these circumstances it uses the Redis RPUSH command, when it should use PUBLISH instead, and as such the subscriber never picks it up.

    EDIT2: Don't forget to clear any cached config with "php artisan config:clear"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。