Laravel echo-无法对专用通道进行身份验证

Laravel echo-无法对专用通道进行身份验证,laravel,socket.io,laravel-echo,Laravel,Socket.io,Laravel Echo,我正在尝试使用专用通道验证laravel echo。每次我收到“无法验证到private-U详细信息。1”。“basket_details”这是通道名称,1是参数 当我使用公共频道时,一切都很完美。因此,我认为auth部分存在一些问题 这是我的错误日志 ⚠ [14:42:55] - 5eKrT28nX7uLHEkLAAAG could not be authenticated to private-basket_details.1 { "message": "", "except

我正在尝试使用专用通道验证laravel echo。每次我收到“无法验证到private-U详细信息。1”。“basket_details”这是通道名称,1是参数

当我使用公共频道时,一切都很完美。因此,我认为auth部分存在一些问题

这是我的错误日志

⚠ [14:42:55] - 5eKrT28nX7uLHEkLAAAG could not be authenticated to private-basket_details.1
{
    "message": "",
    "exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
    "file": "/var/www/html/Laravel/uneek_clothing/trunk/public_html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
    "line": 179,
    "trace": [
        {
            "file": "/var/www/html/Laravel/uneek_clothing/trunk/public_html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 612,
            "function": "match",
            "class": "Illuminate\Routing\RouteCollection",
            "type": "->"
        },
        {
            "file": "/var/www/html/Laravel/uneek_clothing/trunk/public_html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 601,
            "function": "findRoute",
            "class": "Illuminate\Routing\Router",
            "type": "->"
        },
        {
这是我的事件文件

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use App\Basket;
use App\User;

class UpdateWebOrderDetailsToBasket implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $basket;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('basket_details.'.$this->basket->id);
    }

}
这是我的广播服务提供商

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes();

        require base_path('routes/channels.php');
    }
}

可能有问题的一点是您正在侦听的事件名称空间。文档使用点符号而不是反斜杠显示:

Echo.private('basket_details.1')
  // change this to '.App.Events.UpdateWebOrderDetailsToBasket' 
  // or just 'UpdateWebOrderDetailsToBasket' since you are using the default App\Events.
  .listen('.App\Events\UpdateWebOrderDetailsToBasket', (e) => {
    console.log("channel started here");
  });

您能否验证是否达到了正确的授权路径?如果您从以下位置登录,是否打印任何内容:

Broadcast::channel('basket_details.{basketId}', function ($user, $basketId) {
    logger('Basked ID: ' . $basketId);
    return true;
});
例如,如果要使用api中间件保护这些路由,则需要使用适当的头创建Echo实例:

const client = new Echo({
  auth: {
    headers: {
      Authorization: `Bearer ${token}`
    }
  }
})

您是否忘记取消对config/app.php中BroadcastServiceProvider::class的注释

App\Providers\BroadcastServiceProvider::class

添加到您的HTML/布局中,这将在必要的情况下解决您的问题。

记录器未记录,我没有使用任何api
NotFoundHttpException
可能会揭示问题,请尝试从
app/Exceptions/Handler.php
记录异常,以查看是否会显示更具信息性的消息或堆栈跟踪。同样的问题,我以前没有。你找到解决方案了吗?没有,使用了今天的公共频道@Shadrixas,仍然不是解决方案@我也偶然发现了这个问题。我完全不懂这个话题,在这种情况下使用Channel而不是PrivateChannel会有什么坏处?我不得不将我的
socket.io客户端
从版本
3.*
移动到
2.2.0
。对我来说,这一直是个问题。与
Laravel
Laravel echo服务器无关
Broadcast::channel('basket_details.{basketId}', function ($user, $basketId) {
    logger('Basked ID: ' . $basketId);
    return true;
});
const client = new Echo({
  auth: {
    headers: {
      Authorization: `Bearer ${token}`
    }
  }
})
App\Providers\BroadcastServiceProvider::class