为私人渠道正确设置Laravel-echo身份验证的方法

时间:2017-12-26 03:07:42

标签: laravel socket.io laravel-echo

我尝试了很多,但仍然无法正确设置LaravelEcho。实际上我无法设置身份验证步骤。私人频道未进行身份验证。这是larave-echo CLI结果

enter image description here

Boostrap.js

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

import Echo from "laravel-echo"

// window.Pusher = require('pusher-js');
window.io = require('socket.io-client');

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://localhost:6001'
});

Layout.blade.php

Echo.private('App.User.'+id)
                .notification((notification) => {
                    console.log(notification);
  });

laravel回波-server.json

{
    "authHost": "http://bigplan.com",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "*********",
            "key": "*************************"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "localhost"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": false,
    "host": "localhost",
    "port": "6001",
    "protocol": "http",
    "referrers": [],
    "sslCertPath": "",
    "sslKeyPath": "",
    "verifyAuthPath": true,
    "verifyAuthServer": false
}

1 个答案:

答案 0 :(得分:0)

您可能会缺少要进行身份验证的路由,默认情况下它位于routes / channel.php中。如果广播回调已通过身份验证,则应返回true。

这里是一个示例:

Broadcast::channel('notify.{employeeId}', function ($user, $employeeId) {

    $employee = $user->employee;

    if (!$employee){
        return false;
    }

    return $employee->id==$employeeId;

}); 

notify.{employeeId}是您在事件类/

中定义的私人频道名称

$employeeId是您从echo客户端发送的一个,它可以是任何东西,在我的情况下,它是员工ID。

$user是当前经过身份验证的用户。

文档: https://laravel.com/docs/5.5/broadcasting#authorizing-channels