【问题标题】:Authorizing broadcasting channel in an SPA在 SPA 中授权广播频道
【发布时间】:2019-08-28 13:33:32
【问题描述】:

我的项目分为两个app:基于vue的客户端app和基于laravel的服务端rest api app。我在config/app.php 文件中取消了App\Providers\BroadcastServiceProvider::class, 的注释。

默认的广播授权路由是/broadcasting/auth。由于它应用了web 中间件,因此由于 CSRF 问题而显示 419。所以在BroadcastServiceProvider我改变了这个:

Broadcast::routes();

到这里:

Broadcast::routes(['middleware' => ['auth:api']]);

但现在的问题是每当我访问我的客户端应用程序时,我都会在控制台中收到以下错误:

GET http://localhost:8000/v1/login 405(方法不允许)

我该如何解决这个问题?

我的客户端配置:

window.Echo = new Echo({
    authEndpoint: 'http://localhost:8000/broadcasting/auth',
    broadcaster: 'pusher',
    key: 'anyKey',
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true
});

window.Echo.private('test.1').listen('TestUpdated', (e) => {
    /*eslint-disable no-console*/
    console.log(e);
});

【问题讨论】:

    标签: php laravel single-page-application laravel-echo


    【解决方案1】:

    这就是我最终在 api.php 路由文件中所做的:

    Route::post('/broadcast',function (Request $request){
        $pusher = new Pusher\Pusher(env('PUSHER_APP_KEY'),env('PUSHER_APP_SECRET'), env('PUSHER_APP_ID'));
        return $pusher->socket_auth($request->request->get('channel_name'),$request->request->get('socket_id'));
    });
    

    然后我在客户端应用程序中将authEndpoint 更改为该路由:

    window.Echo = new Echo({
        authEndpoint: 'http://localhost:8000/broadcast',
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-10
      • 2017-10-03
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多