php - Laravel 私有(private) channel 未授权 : Error 404

标签 php laravel

我正在尝试构建一个实时通知系统。我使用了 Laravel-Notifications 和 laravel-echo-server。当我在终端 laravel-echo-server start 中使用 start 命令收听通知时,私有(private) channel 发生错误:

Client can not be authenticated, got HTTP status 404 ⚠ [6:57:15 PM] - qXY1YTBcnGUzqJvfAAAB could not be authenticated to private-App.User.2

客户端无法通过身份验证,得到 HTTP 状态 404

代码

channels.php

Broadcast::channel('App.User.{id}', function ($user, $id) {
 return (int) $user->user_id === (int) $id;
});

通知

class StickBoardLiked extends Notification
{
use Queueable;

protected $boardid;


/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct($boardid)
{
    $this->boardid = $boardid;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database', 'broadcast'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->line('The introduction to the notification.')
                ->action('Notification Action', url('/'))
                ->line('Thank you for using our application!');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        //
        'liked_by' => Auth::user()->user_id,
        'username' => Auth::user()->user_name,
        'board_id' => $this->boardid,
        'content_type' => 'like',

    ];
}
}

监听通知的前端脚本

Echo.private("App.User." + this.user.user_id).notification(notification 
=> {
  this.unreadNotifications.push(notification);
});

最佳答案

我已经解决了这个问题,错误出现在幼虫 echo 的路径上。

原来是

http://localhost

应该是

http://localhost:8000

关于php - Laravel 私有(private) channel 未授权 : Error 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54608519/

相关文章:

javascript - 在 WordPress 中创建随机背景颜色

mysql - Laravel 5.4 迁移 key 太长错误

php - Laravel 路由没有到达 Controller

laravel - 如何在Ubuntu Server 18.04中使用系统启动来运行网络套接字?

php - 如何在 PHP 中的图像中写入阿拉伯文文本

php - 解码 while 循环中获取的值

php - SQLSTATE[HY000] [1045] 用户 'landon_app' @'localhost' 的访问被拒绝

php - 在 Laravel 函数中调用多个返回函数

php - 调用PHP函数error_message()时发生 fatal error

php - oci_bind_by_name RETURNING INTO 截断值