Javascript 如何使用Laravel/Pusher/Echo在专用信道中发送有效负载/消息
在花了5个小时试图解决这个问题之后,我快要发疯了,我真的需要一些帮助 我想要实现的是能够以某种形式向客户端发送状态消息。什么样的消息并不重要,只要我能阅读并理解客户机上的消息。我将三个作业分派到Laravel中的一个队列中,我想使用事件类将消息传递给客户机,让他/她知道我们已经走了多远 目前,我可以向客户端发送消息,私有通道正在工作,到目前为止还不错,但我唯一能发送的是用户对象。这一点都没有帮助 bootstrap.js: DeployStatus.php: 我尝试了broadcastWith方法,但没有效果 最后一个是DeploySite.php:Javascript 如何使用Laravel/Pusher/Echo在专用信道中发送有效负载/消息,javascript,php,laravel,pusher,Javascript,Php,Laravel,Pusher,在花了5个小时试图解决这个问题之后,我快要发疯了,我真的需要一些帮助 我想要实现的是能够以某种形式向客户端发送状态消息。什么样的消息并不重要,只要我能阅读并理解客户机上的消息。我将三个作业分派到Laravel中的一个队列中,我想使用事件类将消息传递给客户机,让他/她知道我们已经走了多远 目前,我可以向客户端发送消息,私有通道正在工作,到目前为止还不错,但我唯一能发送的是用户对象。这一点都没有帮助 bootstrap.js: DeployStatus.php: 我尝试了broadcastWith方
如果有人能在正确的方向上帮助我,我将不胜感激。谢谢大家! 广播当然有效。我目前正在我的项目中使用它。尝试完全停止您的项目,运行php artisan optimize:clear,然后再次使用php artisan服务。broadcastWith肯定有效。我目前正在我的项目中使用它。尝试完全停止您的项目,再次运行optimize:clear和php artisan Service。还有,JSON.stringifydata的结果是什么?你能试着安慰一下数据吗?@UzairRiaz噢,伙计。那是我生命中的5个小时,我再也回不来了。优化:清除并解决它。非常感谢!我正要把电脑扔出窗外。没问题,我去过那里,所以我知道该怎么办。我会把它作为一个答案发布,你可以标记它,这样它可以帮助其他人。
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));
});
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];
}
}
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));
}
}