Laravel echo - 无法通过私有(private) channel 进行身份验证

标签 laravel socket.io laravel-echo

我正在尝试使用私有(private) channel 对 laravel 回声进行身份验证。每次我得到 "could not be authenticated to private-basket_details.1 "。 “basket_details”这是 channel 名称,1 是参数。

当我使用公共(public) channel 时,一切正常。所以我认为身份验证部分存在一些问题。

这是我的错误日志

⚠ [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);
    }

}

这是我的 channels.php
<?php
// use Illuminate\Broadcasting\PrivateChannel;

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

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

// Broadcast::PrivateChannel('basket.{basketId}', function ($user, $basketId) {
//     return true;
// });

// Broadcast::channel('basket_details.1', function ($user, $basketId) {
//     return true;
// });

Broadcast::channel('basket_details.{basketId}', function ($user, $basketId) {
    return true;
});

// Broadcast::channel('private-basket_details.*', function ($user, $basketId) {
//     return true;
// });

这是我的 js 文件
Echo.private('basket_details.1')
    .listen('.App\Events\UpdateWebOrderDetailsToBasket', (e) => {
      console.log("channel started here");
    }); 

这是我的 BroadcastServiceProvider
<?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');
    }
}

这是我的 laravel-echo-server.json
{
    "authHost": "http://192.168.1.120:1002",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "be17370a567448f7",
            "key": "7af893ebfa188744e6317b30a481824f"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "9999",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "http://192.168.1.120:9999",
        "allowMethods": "GET,POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

最佳答案

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

App\Providers\BroadcastServiceProvider::class

关于Laravel echo - 无法通过私有(private) channel 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51550545/

相关文章:

php - 如何在 Laravel 中查询的 WHERE 子句中使用正则表达式?

redirect - laravel 重定向到在白屏中显示一条消息

laravel - 如何在 Laravel Validator 中仅显示错误消息

Laravel Echo 无法订阅 Pusher 私有(private) channel

javascript - 如何在 Angular 4 中使用 socket.io-client

javascript - 为什么 socket.io 搞乱了我的 ajax header ?

MySQL : How to identify users I follow or follow me from other person's followers?

javascript - 使用 socket.io 进行服务器端验证

node.js - 在从客户端到 Node 上的服务器的断开连接事件上发出数据

javascript - 在远程服务器中安装 NodeJS 和 SocketIO