- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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/
我正在使用 git clone 部署我的 Laravel 项目并使用 git pull 进行更新 它工作正常,但每次部署时,我都必须从 config/app.php providers 数组和 ali
我是一名优秀的程序员,十分优秀!