我目前正在学习如何正确使用Laravel来回声推送器,但是过去几天我碰壁了,我无法推进也无法解决这个问题。
我不断收到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实例。
这是事件类消息已发布。
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问题上得到了阅读的所有内容
在broadcast.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');
}
答案 0 :(得分:0)
您尝试了吗?
<meta name="csrf-token" content="{{ csrf_token() }}">