Laravel Echo / Pusher 身份验证失败 (403)
Laravel Echo / Pusher authentication fails (403)
学习 Laravel
事件广播 / Echo
/ Vue
并尝试 this tutorial.
我不断收到 403
身份验证响应,我怀疑我对 channels.php
路由缺乏了解是问题所在。对于 Auth
,我使用 Player
模型而不是 User
,它工作正常。
事件ChatMessageSend
class ChatMessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $channel;
public $player;
public $chatMessage;
/**
* Create a new event instance.
* GameChat constructor.
* @param $chatMessage
* @param $player
*/
public function __construct(ChatMessage $chatMessage, Player $player)
{
$this->channel = session()->get('chat.channel');
$this->chatMessage = $chatMessage;
$this->player = $player;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel($this->channel);
}
}
侦听器ChatMessageNotification
(默认/空)
class ChatMessageNotification
{
/**
* ChatMessageNotification constructor.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ChatMessageSent $event
* @return void
*/
public function handle(ChatMessageSent $event)
{
//
}
}
控制器ChatController
class ChatController extends Controller
{
/**
* Send chat message
*
* @param Request $request
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function getMessages(Request $request)
{
return ChatMessage::with('player')
->where('progress_id', '=', session('game.progress.id'))
->orderBy('created_at', 'DESC')
->get();
}
/**
* Send chat message
*
* @param Request $request
* @return array|string
*/
public function sendMessage(Request $request)
{
$player = Auth::user();
$message = $request->input('message');
if ($message) {
$message = ChatMessage::create([
'player_id' => $player->id,
'progress_id' => session()->get('game.progress.id'),
'message' => $request->input('message')
]);
}
broadcast(new ChatMessageSent($player, $message))->toOthers();
return ['type' => 'success'];
}
}
路线channels.php
Broadcast::channel(session()->get('chat.channel'), function ($player, $message) {
return $player->inRoom();
});
在我的 Player
class
/**
* A user can be in one chat channel
*/
public function inRoom()
{
if ((Auth::check()) and ($this->games()->where('progress_id', '=', session('game.progress.id'))->get())) {
return true;
}
return false;
}
当玩家登录时,我在会话中存储了一个我想用作频道的聊天室 ID。
我的 vue
聊天实例是
Vue.component('chat-messages', require('./../generic/chat-messages.vue'));
Vue.component('chat-form', require('./../generic/chat-form.vue'));
const app = new Vue({
el: '#toolbar-chat',
data: {
messages: []
},
created() {
this.fetchMessages();
Echo.private(chat_channel)
.listen('chatmessagesent', (e) => {
this.messages.unshift({
message: e.data.message,
player: e.data.player.nickname
});
});
},
methods: {
fetchMessages() {
axios.get(chat_get_route)
.then(response => {
this.messages = response.data;
});
},
addMessage(message) {
this.messages.unshift(message);
this.$nextTick(() => {
this.$refs.toolbarChat.scrollTop = 0;
});
axios.post(chat_send_route, message)
.then(response => {
console.log(response.data);
});
}
}
});
但我不断收到
POST http://my-games.app/broadcasting/auth 403 (Forbidden)
Pusher : Couldn't get auth info from your webapp : 403
弄清楚您的 routes/channels.php 文件是否按预期运行可能是个好主意。也许添加一些日志记录以查看该路由是否被调用,以及您的 inRoom 函数是否返回您期望的内容。
错误 403 /broadcasting/auth Laravel 版本 > 5.3 & Pusher,您需要更改 resources/assets/js/bootstrap.js 中的代码
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your key',
cluster: 'your cluster',
encrypted: true,
auth: {
headers: {
Authorization: 'Bearer ' + YourTokenLogin
},
},
});
并在app/Providers/BroadcastServiceProvider.php中更改为
Broadcast::routes()
与
Broadcast::routes(['middleware' => ['auth:api']]);
或
Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT
对我有用,希望对你有帮助。
万一还有人需要答案,这对我有用。
添加到您的 broadcastServiceProvider.php
Broadcast::routes([
'middleware' => 'auth:api']);
添加到您的channels.php
Broadcast::channel('chat', function () {
return Auth::check();
});
学习 Laravel
事件广播 / Echo
/ Vue
并尝试 this tutorial.
我不断收到 403
身份验证响应,我怀疑我对 channels.php
路由缺乏了解是问题所在。对于 Auth
,我使用 Player
模型而不是 User
,它工作正常。
事件ChatMessageSend
class ChatMessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $channel;
public $player;
public $chatMessage;
/**
* Create a new event instance.
* GameChat constructor.
* @param $chatMessage
* @param $player
*/
public function __construct(ChatMessage $chatMessage, Player $player)
{
$this->channel = session()->get('chat.channel');
$this->chatMessage = $chatMessage;
$this->player = $player;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel($this->channel);
}
}
侦听器ChatMessageNotification
(默认/空)
class ChatMessageNotification
{
/**
* ChatMessageNotification constructor.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ChatMessageSent $event
* @return void
*/
public function handle(ChatMessageSent $event)
{
//
}
}
控制器ChatController
class ChatController extends Controller
{
/**
* Send chat message
*
* @param Request $request
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function getMessages(Request $request)
{
return ChatMessage::with('player')
->where('progress_id', '=', session('game.progress.id'))
->orderBy('created_at', 'DESC')
->get();
}
/**
* Send chat message
*
* @param Request $request
* @return array|string
*/
public function sendMessage(Request $request)
{
$player = Auth::user();
$message = $request->input('message');
if ($message) {
$message = ChatMessage::create([
'player_id' => $player->id,
'progress_id' => session()->get('game.progress.id'),
'message' => $request->input('message')
]);
}
broadcast(new ChatMessageSent($player, $message))->toOthers();
return ['type' => 'success'];
}
}
路线channels.php
Broadcast::channel(session()->get('chat.channel'), function ($player, $message) {
return $player->inRoom();
});
在我的 Player
class
/**
* A user can be in one chat channel
*/
public function inRoom()
{
if ((Auth::check()) and ($this->games()->where('progress_id', '=', session('game.progress.id'))->get())) {
return true;
}
return false;
}
当玩家登录时,我在会话中存储了一个我想用作频道的聊天室 ID。
我的 vue
聊天实例是
Vue.component('chat-messages', require('./../generic/chat-messages.vue'));
Vue.component('chat-form', require('./../generic/chat-form.vue'));
const app = new Vue({
el: '#toolbar-chat',
data: {
messages: []
},
created() {
this.fetchMessages();
Echo.private(chat_channel)
.listen('chatmessagesent', (e) => {
this.messages.unshift({
message: e.data.message,
player: e.data.player.nickname
});
});
},
methods: {
fetchMessages() {
axios.get(chat_get_route)
.then(response => {
this.messages = response.data;
});
},
addMessage(message) {
this.messages.unshift(message);
this.$nextTick(() => {
this.$refs.toolbarChat.scrollTop = 0;
});
axios.post(chat_send_route, message)
.then(response => {
console.log(response.data);
});
}
}
});
但我不断收到
POST http://my-games.app/broadcasting/auth 403 (Forbidden)
Pusher : Couldn't get auth info from your webapp : 403
弄清楚您的 routes/channels.php 文件是否按预期运行可能是个好主意。也许添加一些日志记录以查看该路由是否被调用,以及您的 inRoom 函数是否返回您期望的内容。
错误 403 /broadcasting/auth Laravel 版本 > 5.3 & Pusher,您需要更改 resources/assets/js/bootstrap.js 中的代码
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your key',
cluster: 'your cluster',
encrypted: true,
auth: {
headers: {
Authorization: 'Bearer ' + YourTokenLogin
},
},
});
并在app/Providers/BroadcastServiceProvider.php中更改为
Broadcast::routes()
与
Broadcast::routes(['middleware' => ['auth:api']]);
或
Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT
对我有用,希望对你有帮助。
万一还有人需要答案,这对我有用。 添加到您的 broadcastServiceProvider.php
Broadcast::routes([
'middleware' => 'auth:api']);
添加到您的channels.php
Broadcast::channel('chat', function () {
return Auth::check();
});