使用laravel echo服务器从redisbroadcaster.php获取accessdeniedhttpexception

5cg8jx4n  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(396)

我不明白为什么我得到这个问题时,试图连接到私人频道。我有100%相同的代码,这篇文章-无法验证laravel专用通道使用laravel回声服务器,redis和socket.io虽然与redis前缀不工作。。。它适用于简单通道。。。这是我的错误-
客户端无法通过身份验证,已通过laravel echo服务器获得http状态403
这是我的代码-

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    auth: {
    headers:
        {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    }
});
window.Echo.private('user.'+userIdentifier).listen('SomeTestEvent', function (e) {
...
}

路由/频道.php-

Broadcast::channel('user.{id}', function ($user, $id) {
    return true; // returning always true just for test
});

广播服务提供商.php-

public function boot()
    {
        Broadcast::routes();

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

事件/sometestevent.php-

public function broadcastOn()
    {
        return new PrivateChannel('user.'.$this->data['user_id']);
    }

laravel-echo-server.json文件-

{
    "authHost": "http://localhost:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "e0ebecd04673b905",
            "key": "39c2a1314e4ef8e2879486a4a3b91c1e"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "host": "localhost",
            "port": "6379"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "*",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}
ax6ht2ek

ax6ht2ek1#

天哪,我只是想知道怎么做。
我需要用- Broadcast::routes([ 'middleware' => 'auth:api' ]); 我在前端添加了授权令牌-

auth: {
    headers:
    {
        'Authorization': 'Bearer ' + accToken,
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
}

相关问题