1

我看到这个问题已被多次询问,但没有一个答案有效,所以我正在寻求帮助和一些见解。

我正在使用 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": ""
}

当我触发我的事件时,我会在控制台中收到该事件,但由于某种原因无法对用户进行身份验证,我无法弄清楚。

4

1 回答 1

1

您是否在 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;
});

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

于 2017-09-29T14:32:31.970 回答