gpt4 book ai didi

php - Pusher 不在私有(private) channel 上广播 -PHP/Laravel

转载 作者:行者123 更新时间:2023-12-02 00:41:44 25 4
gpt4 key购买 nike

我已经为我的应用设置了 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) channel :

Broadcast::channel('items', function ($user, $item) {
return true;
});

应用程序/资源/ 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 Echo 注册:(事件触发 View 从我的主要布局的“头部”部分扩展。)

<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) channel 广播,但是当我尝试在私有(private) channel 上广播时,我得到

  //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 错误代码。

认为这可能有助于找出问题。

我该怎么办?

最佳答案

所以我弄清楚了我应该如何实现私有(private) channel 收听并让它发挥作用。

我的错误在于通配符的选择。

我告诉它使用经过身份验证的用户 ID 作为通配符,而不是应用更改的项目 ID。

因此,下面的 channel 注册总是返回 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);
}
}

channel 注册:

app\routes\channels.php    

Broadcast::channel('items.{item}', function ($user, \App\Item $item) {
return $user->id === $item->user_id;
});

Laravel Echo 注册:

<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>

谢谢大家指点。

关于php - Pusher 不在私有(private) channel 上广播 -PHP/Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46225636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com