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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答