[英]How to send a payload/message in private channel with Laravel / Pusher / Echo
I'm about to go mad after spending the last 5 hours trying to figure this out and I could really use some help.在花了最后 5 个小时试图解决这个问题之后,我快要 go 发疯了,我真的需要一些帮助。
What I want to achieve is to be able to send a status-message to the client in some form.我想要实现的是能够以某种形式向客户端发送状态消息。 It's not important what kind of message as long as I can read it and understand the message on the client.只要我能阅读并理解客户端上的消息,什么样的消息并不重要。 I'm dispatching three jobs to a queue in Laravel and I would like to use the Event class to relay a message to the client along the way to let him/her know how far we've come.我正在将三个作业分派到 Laravel 中的一个队列,我想使用事件 class 沿途向客户中继一条消息,让他/她知道我们已经走了多远。
At the moment I can send a message to the client, the private channel is working and so far so good, but the only thing I manage to send is the user-object.目前我可以向客户端发送消息,专用通道正在运行并且到目前为止一切正常,但我唯一设法发送的是用户对象。 Which is not helpful at all.这根本没有帮助。
bootstrap.js :引导程序.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 :部署状态.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];
}
}
(I tried with the broadcastWith() method, but no effect) (我尝试了 broadcastWith() 方法,但没有效果)
And the last one DeploySite.php :最后一个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));
}
}
I would really appreciate if someone could help me in the right direction.如果有人能在正确的方向上帮助我,我将不胜感激。 Thank you!谢谢!
broadcastWith()
certainly works. broadcastWith()
当然有效。 I am currently using it in my project.我目前正在我的项目中使用它。 Try stopping your project completely, run php artisan optimize:clear
and php artisan serve
again.尝试完全停止您的项目,再次运行php artisan optimize:clear
和php artisan serve
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.