当前位置: 首页 > 知识库问答 >
问题:

无法使用laravel echo服务器、redis和socket对laravel专用通道进行身份验证。木卫一

浦修文
2023-03-14

我使用Laravel回声服务器,redis和socket.io聊天消息。

我正在用电视广播这个事件。

event(new MessageSent($messageThread));

这是一个私人频道,其方法为brodcastOn,如下所示:

   public function broadcastOn()
    {
        return new PrivateChannel('message.' . $this->broadcastUser->id);
    }

我的路线/频道。php看起来像:

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

下面是我的laravel echo服务器。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的my redis部分。php

'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个答案

董永宁
2023-03-14

而不是

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

用这个说法

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

在Echo的命名空间前使用点(.)

实例

window.Echo(...)
.listen('.namespace')

并在通知类中重写broadcastAs,然后使用shouldBroadCastNow

broadcastAs(){
return 'namespace'
}
方鸿振
2023-03-14

您有两个问题:

  • 在Echo端,您应该使用
window.Echo.private(`message.${userId}`);

您还必须确保Laravel配置变量database.redis.options.prefix设置为"

  • 在Laravel端,您需要实现一个通道的授权,以便允许套接字监听这个私有通道
Broadcast::channel('message.{userId}', function ($user, $userId) {
    //Your authorization logic which should return a boolean
});

https://laravel.com/docs/7.x/broadcasting#defining-授权回调

 类似资料:
  • 我试图通过Laravel发送电子邮件。但它正在出错 错误获取: 使用3个可能的身份验证器在用户名为“myEmailID@gmail.com”的SMTP服务器上进行身份验证失败。身份验证器登录返回Swift_TransportException:预期响应代码235,但得到代码“535”,消息“535-5.7.8用户名和密码不被接受”。在535 5.7.8https://support.google.

  • 我正在使用redis和socket与laravel echo合作。木卫一。它在显示和专用通道中失败 我将此软件包用于我的服务器laravel echo服务器 当我使用公共频道时,一切正常,但当我使用私人频道时,以下错误记录在laravel echo服务器中 下面是我的应用程序。js,bootstrap。js和laravel echo服务器。json bootstrap.js 拉威尔回声服务器。js

  • GoogleCredential凭证=newGoogleCredential.Builder(). setTransfer(TRANSPORT). setJsonFactory(JSON_FACTORY). setServiceAccount tId("SOMETHING@developer.gserviceaccount.com"). setServiceAccount tScopes(Bigq

  • 我正在尝试在我的Laravel 6应用程序中使用smtp电子邮件功能。 有趣的是: 它在我的本地主机中工作正常 它也在我的服务器中工作(没有超文本传输协议) 当我在服务器中应用https时,它停止工作。 它给了我以下错误: 使用3个可能的身份验证器在用户名"*********@gmail.com的SMTP服务器上进行身份验证失败。身份验证器LOGIN返回预期响应代码235,但得到代码“534”,并

  • 我正在尝试配置laravel的auth以适合我的db。但无论我做什么,都会覆盖像或在LoginController中,它忽略所有内容。有人知道如何用不同的数据库参数化laravel的身份吗? 以下是我在LoginController中更改的内容: 在用户模型中: 编辑:config/auth.php: ]; LoginController: 名称空间App\Http\Controllers\Aut