gpt4 book ai didi

拉拉维尔。每个用户的多个 channel 或个人 channel ?

转载 作者:行者123 更新时间:2023-12-05 06:23:19 24 4
gpt4 key购买 nike

我在 Laravel 中使用事件广播。我正在使用基于角色的通知访问权限。我有用于广播的自定义 auth guard。当用户连接到 channel 时,客户端将具有内部权限的 access_token 发送到服务器。服务器验证此 access_token 并允许连接(我使用 3d 方服务 Auth0 对用户进行身份验证,这意味着每次用户连接到每个广播 channel 时,都需要一些时间在服务器端验证 token )。

我的应用程序中有多个要广播的事件。我是否必须为每个用户创建多个私有(private) channel (每个事件一个),例如:

window.Echo.private('channel1'+user_id)
// This channel can translate only one event - "firstEvent"
.listen("firstEvent", (mes) => {
console.log(mes);
})
window.Echo.private('channel2'+user_id)
// This channel can translate only one event - "secondEvent"
.listen("secondEvent", (mes) => {
console.log(mes);
})
// etc.

然后在服务器端我检查(在 routes/channels.php 关闭)用户是否有这个 channel 的权限,并允许或禁止他连接到 channel 。当事件触发时,在它的 broadcastOn 中我会做类似的事情:

public function broadcastOn()
{
$users_from_db = \App\NotificationUsers::where('permission',$this->permission)->get();
foreach($users_from_db as $user){
return new PrivateChannel('connections'.$user->id);
}

}
// Permission for each event is defined in each event class as private property.

因此每个初始页面加载的用户都尝试连接到多个 channel ,如果他允许,他会收到通知,不允许 - 不接收。

或者

我应该创建一个 channel :

window.Echo.private('singleChannel.'+user_id)
.listen("Event1", (mes) => {
console.log(mes);
})
window.Echo.private('singleChannel.'+user_id)
.listen("Event2", (mes) => {
console.log(mes);
})

或者...任何其他变体?什么是好的做法?

最佳答案

broadcastOn方法返回 channel 或数组,在你的“foreach”中你不能使用“return”你应该在“foreach”之后为每个用户填充一个私有(private) channel 的数组对象你必须返回数组。

public function broadcastOn()
{
$users_from_db = \App\NotificationUsers::where('permission',$this->permission)->get();
$channels = [];
foreach($users_from_db as $user){
$channels[] = new PrivateChannel('connections'.$user->id);
}

return $channels;

关于拉拉维尔。每个用户的多个 channel 或个人 channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496339/

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