过去5个小时试图弄清这一点后,我快要生气了,我可以真的使用一些帮助。
我想要实现的是能够以某种形式向客户端发送状态消息。只要能阅读并理解客户端上的消息,哪种消息就无关紧要。我正在Laravel中将三个作业分派到一个队列中,我想使用Event类在向客户传递消息的过程中让他/她知道我们已经走了多远。
目前,我可以向客户端发送消息,专用通道正在运行,到目前为止一切正常,但是我唯一要发送的是用户对象。这根本没有帮助。
bootstrap.js :
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'xxxxxx',
cluster: 'eu',
forceTLS: true
});
var channel = window.Echo.private('user.'+ window.Laravel.user); //
channel.listen('.status', function(data) {
console.log(JSON.stringify(data));
});
DeployStatus.php :
namespace App\Events;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class DeployStatus implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function broadcastOn()
{
return new PrivateChannel('user.'.$this->user->id);
}
public function broadcastAs()
{
return 'status';
}
public function broadcastWith()
{
return ['id' => $this->user->id];
}
}
(我尝试了broadcastWith()方法,但没有效果)
最后一个 DeploySite.php :
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Events\DeployStatus;
class DeploySite implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
}
public function handle()
{
$user = auth()->user();
event(new DeployStatus($user));
}
}
如果有人能在正确的方向上帮助我,我将不胜感激。谢谢!
答案 0 :(得分:1)
broadcastWith()
当然可以。我目前在我的项目中使用它。尝试完全停止您的项目,然后再次运行php artisan optimize:clear
和php artisan serve
。