【发布时间】:2017-12-06 13:39:52
【问题描述】:
我在网站上广播 cmets 时遇到问题: - 有时事件监听器处理了事件,但没有广播事件。
\App\Http\Controllers\CommentController 在方法“store”中:
$comment = Comment::create($request->all());
broadcast(new NewCommentAdded($comment));
\App\Events\NewCommentAdded
namespace App\Events;
use App\Models\Comment;
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 NewCommentAdded implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $comment;
public $username;
public $userimage;
public $usergroup;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Comment $comment)
{
$this->comment = $comment;
$this->username = $comment->user->name;
$this->userimage = $comment->user->image;
$this->usergroup = $comment->user->role_id;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
\Log::info('AddComment event broadcasted');
return ['webinar_'.$this->comment->webinar_id];
}
public function broadcastAs()
{
return 'comment';
}
}
\App\Listeners\BroadcastNewComment
namespace App\Listeners;
use App\Events\NewCommentAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class BroadcastNewComment
{
// use InteractsWithQueue;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* @param NewCommentAdded $event
* @return void
*/
public function handle(NewCommentAdded $event)
{
\Log::info('Added New Comment Event fired successfully');
}
}
在这一切之后,我得到了日志:
[2017-12-06 16:02:03] production.INFO: Added New Comment Event fired successfully
[2017-12-06 16:02:03] production.INFO: AddComment event broadcasted
[2017-12-06 16:03:23] production.INFO: Added New Comment Event fired successfully
[2017-12-06 16:03:24] production.INFO: AddComment event broadcasted
[2017-12-06 16:03:58] production.INFO: Added New Comment Event fired successfully
[2017-12-06 16:17:26] production.INFO: Added New Comment Event fired successfully
[2017-12-06 16:17:27] production.INFO: AddComment event broadcasted
如您所见,事件侦听器处理的事件:[2017-12-06 16:03:58] production.INFO:已成功触发添加新评论事件 但是事件没有广播。
我的环境:
BROADCAST_DRIVER=redis
QUEUE_DRIVER=beanstalkd
我的主管配置:
[program:ta-production-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/domains/data/www/somedomain.com/artisan queue:work --daemon --sleep=3 --tries=5
autostart=true
autorestart=true
user=root
numprocs=5
redirect_stderr=true
stdout_logfile=/var/www/domains/data/www/somedomain.com/storage/logs/worker.log
【问题讨论】:
-
您是否已将侦听器配置为侦听该事件?这可以在
EventServiceProvider.php中完成 -
我当然做到了
'App\Events\NewCommentAdded' => [ 'App\Listeners\BroadcastNewComment', ],'App\Events\CommentDeleted' => [ 'App\Listeners\BroadcastDeleteComment', ], -
我只是偶尔看到问题,而不是在每个事件中
-
我也有同样的问题,queue driver是beanstalkd,当事件触发时,beanstalkd中出现job,但是没有被队列处理,job被hold
-
停止使用Supervisor
标签: laravel redis queue beanstalkd broadcasting