首页 > 解决方案 > Laravel 广播通知错误:客户端无法通过身份验证,获得 HTTP 状态 404

问题描述

我正在尝试构建一个实时通知系统。我使用了 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);
        });

标签: phplaravellaravel-echolaravel-broadcast

解决方案


它可能与您的回声服务器auth host配置有关,

您可以使用 laravel echo server 和 redis 检查例如 laravel 实时应用程序,

Laravel / Redis / Socket.IO / Laravel-Echo 的实时应用示例

我希望这个能帮上忙


推荐阅读