gpt4 book ai didi

laravel - 无法使用 Laravel echo server、redis 和 socket.io 验证 laravel 私有(private) channel

转载 作者:行者123 更新时间:2023-12-03 06:42:36 35 4
gpt4 key购买 nike

我将 Laravel Echo 服务器、redis 和 socket.io 用于聊天消息。

我正在使用广播事件。

event(new MessageSent($messageThread));

这是使用方法 brodcastOn 的私有(private) channel 如下:
   public function broadcastOn()
{
return new PrivateChannel('message.' . $this->broadcastUser->id);
}

我的 routes/channels.php好像:
Broadcast::channel('message.*', function ($user, $id) {
return (int) $user->id === (int) $id;
});

以下是我的 laravel-echo-server.json :
{
"authHost": "http://localhost:8000",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "******",
"key": "*************"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port":"6379",
"host":"127.0.0.1",
"db":"0",
"password":"****"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
}

用于监听事件的 jQuery:
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
client: socketio,
auth: {headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr("content"),
}},
});

var userId = window.APP.user;

const channel = window.Echo.channel(`app_database_private-message.${userId}`);

channel.listen('MessageSent', (payload) => {
console.log('payload', payload)
});


这是来自 config/database.php 的我的 redis 部分:
'redis' => [

'client' => env('REDIS_CLIENT', 'predis'),

'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
'prefix' => Str::slug(env('APP_NAME', 'laravel'), '_').'_database_',
],

'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],

'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],

],

它显示以下错误:
L A R A V E L  E C H O  S E R V E R

version 1.6.0

⚠ Starting server in DEV mode...

✔ Running at localhost on port 6001
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...

Server ready!

[1:21:22 PM] - Preparing authentication request to: http://localhost:8000
[1:21:22 PM] - Sending auth request to: http://localhost:8000/broadcasting/auth

⚠ [1:21:22 PM] - IQ7eT9P_RzF62lRSAAAA could not be authenticated to app_database_private-message.3
{ "message": "", "exception": "Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException", "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php", "line": 81, "trace": [ { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php", "line": 58, "function": "verifyUserCanAccessChannel", "class": "Illuminate\\Broadcasting\\Broadcasters\\Broadcaster", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php", "line": 319, "function": "auth", "class": "Illuminate\\Broadcasting\\Broadcasters\\RedisBroadcaster", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php", "line": 237, "function": "__call", "class": "Illuminate\\Broadcasting\\BroadcastManager", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastController.php", "line": 23, "function": "__callStatic", "class": "Illuminate\\Support\\Facades\\Facade", "type": "::" }, { "function": "authenticate", "class": "Illuminate\\Broadcasting\\BroadcastController", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php", "line": 54, "function": "call_user_func_array" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php", "line": 45, "function": "callAction", "class": "Illuminate\\Routing\\Controller", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 219, "function": "dispatch", "class": "Illuminate\\Routing\\ControllerDispatcher", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 176, "function": "runController", "class": "Illuminate\\Routing\\Route", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 680, "function": "run", "class": "Illuminate\\Routing\\Route", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 30, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "/opt/edugem/apps/project/app/Http/Middleware/AppMiddleware.php", "line": 55, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "App\\Http\\Middleware\\AppMiddleware", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php", "line": 41, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php", "line": 75, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php", "line": 49, "function": "Illuminate\\Routing\\{closure}"

最佳答案

你有2个问题:

  • 在 Echo 方面,您应该使用

  • window.Echo.private(`message.${userId}`);

    您还必须确保 Laravel 配置变量 database.redis.options.prefix设置为 '' .
  • 在 Laravel 方面,您需要实现 channel 的授权以允许套接字监听此私有(private) channel 。

  • Broadcast::channel('message.{userId}', function ($user, $userId) {
    //Your authorization logic which should return a boolean
    });

    https://laravel.com/docs/7.x/broadcasting#defining-authorization-callbacks

    关于laravel - 无法使用 Laravel echo server、redis 和 socket.io 验证 laravel 私有(private) channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61224586/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com