【问题标题】:419 auth error trying to set up an Event with Laravel Echo, Vue, and Pusher419 auth 错误尝试使用 Laravel Echo、Vue 和 Pusher 设置事件
【发布时间】:2019-01-17 12:08:03
【问题描述】:

我目前正在学习如何正确使用 Laravel echo an pusher,但是这几天碰壁了,我无法前进也无法解决这个问题。

我不断收到 419 错误

/broadcasting/auth 419 (proxy reauthentication required)

但我看不出问题出在哪里

我在控制器中有一个方法可以在插入消息后触发事件

/** The headers of the controller class **/

use App\Message;
use App\User;
use Auth;
use App\Events\MessagePosted;


/*The event that is fired*/

event ( new MessagePosted($msg, Auth::user()) );

其中 $msg 是一个 Message 实例。

这是事件类 Message Posted。

class MessagePosted implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Message
     *
     * @var Message
     */
     public $message;

    /**
     * User
     *
     * @var User
     */
     public $user;

    public function __construct(Message $message, User $user)
    {
        $this->message = $message;
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PresenceChannel('chatroom.'.$this->message);
    }
}

这就是我的 channels.php 中的内容

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

Broadcast::channel('chatroom.{$message}', function ($user, $message){
    return $user->id == $message->id_sender || $user->id == $message->id_receiver;
});

除此之外,我不知道是什么导致了错误,我得到了我在 laravel 文档和一些 SO 问题上阅读的所有内容

在 broadcasting.php 上我得到了:

'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => true,
            ],
        ],
...
];

在 bootstrap.js 上,我取消了文档所述行的注释:

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key : "I PLACED MY APP KEY DIRECTY HERE"
    cluster : "us2"
});

在我的广播服务提供商上

public function boot()
{
    Broadcast::routes();

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

【问题讨论】:

    标签: php laravel vue.js vuejs2 laravel-echo


    【解决方案1】:

    你试过了吗?

    <meta name="csrf-token" content="{{ csrf_token() }}">
    

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2019-09-14
      • 2020-09-15
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2021-07-02
      • 2017-04-15
      • 2021-05-30
      相关资源
      最近更新 更多