Laravel 5.4 已触发但未广播的事件
Laravel 5.4 Events fired, but not broadcasted
我在网站上广播评论时遇到问题:
- 有时事件侦听器处理事件,但事件未广播。
\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
问题的解决方法:
- 停止使用 Supervisor 或
- 降级主管或
- 升级主管。
我停止使用 Supervisor 并且一切正常。
我在网站上广播评论时遇到问题: - 有时事件侦听器处理事件,但事件未广播。
\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
问题的解决方法:
- 停止使用 Supervisor 或
- 降级主管或
- 升级主管。
我停止使用 Supervisor 并且一切正常。