我正在尝试构建一个实时通知系统。我使用了 Laravel-Notifications 和 laravel-echo-server。当我在终端 laravel-echo-server start 中使用 start 命令收听通知时,私人频道发生错误:
⚠ [下午 4:36:43] - H12aWYBsF5UdR5NYAAAH 无法通过 private-App.Domain.User.User.1 的身份验证
客户端无法通过身份验证,获得 HTTP 状态 404
代码
// 频道.php
Broadcast::channel('App.Domain.User.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;});
// 通知文件代码 (ListingApproved.php)
class ListingApproved extends Notification implements ShouldQueue {
use Queueable;
private $listing;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Listing $listing)
{
$this->listing = $listing;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
public function toArray($notifiable)
{
return $this->listing->id;
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'invoice_id' => $this->listing->id
]);
}}
//监听通知的前端脚本
Echo.private('App.Domain.User.User.' + '{{ auth()->user()->id }}')
.listen(".Illuminate\\Notifications\\Events\\BroadcastNotificationCreated", (notification) => {
console.log('yes');
console.log(notification.type);
});