1

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

在此处输入图像描述

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'
});

布局.blade.php

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

laravel-echo-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
}
4

1 回答 1

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}是您在 Event 类中定义的私人频道名称/

$employeeId这是您从 echo 客户端发送的那个,它可以是任何东西,在我的例子中,它是员工 ID。

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

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

于 2019-08-13T06:26:37.540 回答