Laravel Echo Client无法通过身份验证

时间:2017-09-29 13:58:24

标签: laravel laravel-echo

我看到这个问题已被多次询问,但没有答案正在发挥作用,所以我正在寻求帮助并对此有所了解。

我正在使用laravel echo和redis来播放事件。我的laravel echo服务器很好用,redis运行得很好。

当我重新加载我的网页时,我在laravel-echo服务器终端中收到此错误:

⚠ [4:49:19 PM] - qg4WSWl5YusQY8hJAAAA could not be authenticated to private-test-channel.1
{"error":"Unauthenticated."}
Client can not be authenticated, got HTTP status 401

我和它一起来回走动,并且没有帮助它。

这是我的app.js

import Echo from "laravel-echo";

window.Echo = new Echo({
  broadcaster: 'socket.io',
  host: window.location.hostname + ':6001'
});

这是我的echo.js

  window.Echo.private('test-channel.{{Auth::user()->id}}')
    .listen('EventName', function(event) {
      console.log('data', event);
  });

我的BroadcastServiceProvider

public function boot()
{
  //broadcast routes middleware
  Broadcast::routes(['middleware' => ['auth', 'web']]);

  /*
  * Authenticate the user's personal channel...
  */
  Broadcast::channel('App.User.*', function ($user, $userId) {
    return (int) $user->id === (int) $userId;
  });

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

这是我的larave-echo-server.json

{
    "authHost": "http://127.0.0.1:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "570453f474365cc8",
            "key": "4baec74e662696009e7857e49d3364c4"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "127.0.0.1",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": ""
}

当我触发我的事件时,我在控制台中获取了该事件,但由于某种原因无法对用户进行身份验证,我无法弄明白。

1 个答案:

答案 0 :(得分:0)

您是否在routes / channels.php中看到了许可?

这是解释的链接:https://laravel.com/docs/5.5/broadcasting#authorizing-channels 我看到你放了BroadcastServiceProvider

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

但是在routes / channels.php中,默认命令是:

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

也许是在制造一些冲突或问题