Laravel广播事件没有解雇

Mwi*_*Tim 1 php events laravel laravel-5

每次创建新的市场快照时,我都会尝试广播一个事件.我在这里遵循了laravel教程.

但似乎我遗漏了一些东西,因为即使没有创建日志:

    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');
        }
    }
Run Code Online (Sandbox Code Playgroud)

我甚至在EventServiceProvider中注册了事件(未在laravel链接上提及)

protected $listen = [
    'App\Events\NewMarketSnapshot' => [
      'App\Listeners\NewMarketSnapshotListener',
    ],
];
Run Code Online (Sandbox Code Playgroud)

正在调用处理程序,但事件未被广播.

    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.');
        }
    }
Run Code Online (Sandbox Code Playgroud)

使用简单的路线进行测试:

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

我的.env文件中的行

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

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Run Code Online (Sandbox Code Playgroud)

Mat*_*aly 5

我注意到您使用Redis作为队列和广播驱动程序.我的经验是使用它来打破广播(基本上它在广播事件上向Redis发送了错误的命令).

如果您将队列切换到不同的驱动程序或使用单独的Redis实例,它应该可以正常工作.

编辑:我相信在这种情况下它使用Redis RPUSH命令,而它应该使用PUBLISH,因此订阅者永远不会选择它.

EDIT2:别忘了用"php artisan config:clear"清除任何缓存的配置

  • 另外,不要忘记使用`php artisan config:clear`清除任何缓存的配置 (2认同)