我在网站上播放评论有问题: - 有时事件监听器处理事件,但事件未被广播。
\ App \ Http \ Controllers \ CommentController in method" store":
$comment = Comment::create($request->all());
broadcast(new NewCommentAdded($comment));
\应用\活动\ 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';
}
}
\应用\监听\ 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
正如您所见,Event Listener处理事件: [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
答案 0 :(得分:-1)
问题的解决方案:
我停止使用主管,一切都很完美。