如何使用 Laravel / Pusher / Echo 在私人频道中发送 payload/message
How to send a payload/message in private channel with Laravel / Pusher / Echo
在花了最后 5 个小时试图解决这个问题后,我快要发疯了,我可以真的需要一些帮助。
我想要实现的是能够以某种形式向客户端发送状态消息。只要我能阅读并理解客户端上的消息,什么样的消息并不重要。我正在将三个作业分派到 Laravel 中的一个队列,我想使用事件 class 沿途向客户端中继一条消息,让 him/her 知道我们走了多远来了
目前我可以向客户端发送消息,专用通道正在运行并且到目前为止一切正常,但我唯一设法发送的是用户对象。这根本没有帮助。
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));
}
}
如果有人能在正确的方向上帮助我,我将不胜感激。谢谢!
broadcastWith()
当然有效。我目前正在我的项目中使用它。尝试完全停止您的项目,再次 运行 php artisan optimize:clear
和 php artisan serve
。
在花了最后 5 个小时试图解决这个问题后,我快要发疯了,我可以真的需要一些帮助。
我想要实现的是能够以某种形式向客户端发送状态消息。只要我能阅读并理解客户端上的消息,什么样的消息并不重要。我正在将三个作业分派到 Laravel 中的一个队列,我想使用事件 class 沿途向客户端中继一条消息,让 him/her 知道我们走了多远来了
目前我可以向客户端发送消息,专用通道正在运行并且到目前为止一切正常,但我唯一设法发送的是用户对象。这根本没有帮助。
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));
}
}
如果有人能在正确的方向上帮助我,我将不胜感激。谢谢!
broadcastWith()
当然有效。我目前正在我的项目中使用它。尝试完全停止您的项目,再次 运行 php artisan optimize:clear
和 php artisan serve
。