Pusher 不在私人频道上广播 -PHP/Laravel
Pusher Doesn't Broadcast on Private Channels -PHP/Laravel
我已经为我的应用设置了 Pusher 和 Laravel Echo,以便在某些事件触发时通知用户。
我已经通过在 "Public Channel" 上广播来测试安装程序是否正常工作,并成功地看到它是有效的。
这是事件本身:
class ItemUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $item;
public function __construct(Item $item)
{
$this->item = $item;
}
public function broadcastOn()
{
return new Channel('items');
}
}
Public频道:
Broadcast::channel('items', function ($user, $item) {
return true;
});
app/resources/assets/js/bootstrap.js:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'my_key',
cluster: 'eu',
encrypted: true
});
和 Laravel 回声注册:(事件触发视图从我的主布局的 "head" 部分扩展。)
<head>
<meta charset="utf-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="{{ asset('js/app.js') }}"></script>
<script>
window.Laravel = {'csrfToken': '{{csrf_token()}}'}
window.Echo.channel('items')
.listen('ItemUpdated', function(e) {
console.log("It is working:)");
});
</script>
</head>
现在,此设置适用于 public 频道广播,但是当我尝试在私人频道上广播时,我得到
//updated "ItemUpdated" event broadcastOn() method
public function broadcastOn()
{
return new PrivateChannel('items');
}
//updated laravel echo registering
<script>
window.Laravel = {'csrfToken': '{{csrf_token()}}'}
window.Echo.private('items')
.listen('ItemUpdated', function(e) {
console.log("It is working:)");
});
//console logged error
Pusher couldn't get the auth user from :myapp 500
我检查了来自开发者控制台的传出网络请求,发现推送器正在尝试 "POST" 到
http://localhost:8000/broadcast/auth
但它收到 500 错误代码。
可能有助于解决问题的想法。
我该怎么办?
所以我想出了我应该如何实现私有频道监听并让它工作。
我的错误在于通配符的选择。
我告诉它使用经过身份验证的用户 ID 作为通配符,而不是应用更改的项目 ID。
因此,下面的频道注册总是返回 false 并导致 Pusher 抛出 500 auth couldn't found。
Broadcast::channel('items.{item}', function ($user, \App\Item $item) {
return $user->id === $item->user_id;
});
所以,这是工作版本:
正在广播的事件:
//ItemUpdated event
class ItemUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $item;
public function __construct(Item $item)
{
$this->item = $item;
}
public function broadcastOn()
{
return new PrivateChannel('items.'.$this->item->id);
}
}
频道注册:
app\routes\channels.php
Broadcast::channel('items.{item}', function ($user, \App\Item $item) {
return $user->id === $item->user_id;
});
Laravel回声注册:
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
<script>
window.Laravel = {'csrfToken': '{{csrf_token()}}'}
window.Echo.private(`items.{{{$item->id}}}`)
.listen('ItemUpdated', function(e) {
console.log("It is working!");
});
</script>
谢谢大家指点
我已经为我的应用设置了 Pusher 和 Laravel Echo,以便在某些事件触发时通知用户。
我已经通过在 "Public Channel" 上广播来测试安装程序是否正常工作,并成功地看到它是有效的。
这是事件本身:
class ItemUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $item;
public function __construct(Item $item)
{
$this->item = $item;
}
public function broadcastOn()
{
return new Channel('items');
}
}
Public频道:
Broadcast::channel('items', function ($user, $item) {
return true;
});
app/resources/assets/js/bootstrap.js:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'my_key',
cluster: 'eu',
encrypted: true
});
和 Laravel 回声注册:(事件触发视图从我的主布局的 "head" 部分扩展。)
<head>
<meta charset="utf-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="{{ asset('js/app.js') }}"></script>
<script>
window.Laravel = {'csrfToken': '{{csrf_token()}}'}
window.Echo.channel('items')
.listen('ItemUpdated', function(e) {
console.log("It is working:)");
});
</script>
</head>
现在,此设置适用于 public 频道广播,但是当我尝试在私人频道上广播时,我得到
//updated "ItemUpdated" event broadcastOn() method
public function broadcastOn()
{
return new PrivateChannel('items');
}
//updated laravel echo registering
<script>
window.Laravel = {'csrfToken': '{{csrf_token()}}'}
window.Echo.private('items')
.listen('ItemUpdated', function(e) {
console.log("It is working:)");
});
//console logged error
Pusher couldn't get the auth user from :myapp 500
我检查了来自开发者控制台的传出网络请求,发现推送器正在尝试 "POST" 到
http://localhost:8000/broadcast/auth
但它收到 500 错误代码。
可能有助于解决问题的想法。
我该怎么办?
所以我想出了我应该如何实现私有频道监听并让它工作。
我的错误在于通配符的选择。
我告诉它使用经过身份验证的用户 ID 作为通配符,而不是应用更改的项目 ID。
因此,下面的频道注册总是返回 false 并导致 Pusher 抛出 500 auth couldn't found。
Broadcast::channel('items.{item}', function ($user, \App\Item $item) {
return $user->id === $item->user_id;
});
所以,这是工作版本:
正在广播的事件:
//ItemUpdated event
class ItemUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $item;
public function __construct(Item $item)
{
$this->item = $item;
}
public function broadcastOn()
{
return new PrivateChannel('items.'.$this->item->id);
}
}
频道注册:
app\routes\channels.php
Broadcast::channel('items.{item}', function ($user, \App\Item $item) {
return $user->id === $item->user_id;
});
Laravel回声注册:
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
<script>
window.Laravel = {'csrfToken': '{{csrf_token()}}'}
window.Echo.private(`items.{{{$item->id}}}`)
.listen('ItemUpdated', function(e) {
console.log("It is working!");
});
</script>
谢谢大家指点