下面是为我的应用程序admins、designers、customers等定义的保护程序,默认的保护是designer guard。
我希望每个guard都有自己的private channel。因此,我在channel.php中为每个条目定义了多个条目,如下所示
Broadcast::channel('private.admins.{id}', function ($admin, $id) {
Log::info($admin);
//logging the admin
});但是这始终是带有binding类的default guard类,所以我的问题是如何告诉它如何在这里使用Admin model。我哪儿都找不到它。你能把我引向正确的方向吗?
实际上,我希望每个guard都有自己的private channel。
发布于 2018-06-24 06:08:29
尝试更改BroadcastServiceProvider文件app\Providers\BroadcastServiceProvider.php
每个警卫的不同广播终端
public function boot()
{
//Broadcast::routes();
//match any of the 3 auth guards
Broadcast::routes(['middleware' => ['web','auth:admins,designers,customers']]);
require base_path('routes/channels.php');
}现在在channels.php
Broadcast::channel('admins.channel.{id}', function ($model, $id) {
return $model->id === $id && get_class($model) === 'App\Admin';
});
Broadcast::channel('designers.channel.{id}', function ($model, $id) {
return $model->id === $id && get_class($model) === 'App\Designer';
});
Broadcast::channel('customers.channel.{id}', function ($model, $id) {
return $model->id === $id && get_class($model) === 'App\Customer';
});发布于 2020-07-18 08:40:59
我贴出这个答案,对于每一个可能面临这个问题的人来说都是不可能的。我用的是拉里7和beyondcode/laravel-websockets。当我深入挖掘源代码时,在BoradcastServiceProvider.php中指定中间件将无法工作。定义通道保护的唯一方法是指定通道的选项:
Broadcast::channel('messaging.organ.{id}', function ($organ , $id) {
return $organ->id == $id && get_class($organ) === "App\Organization";
} , ['guards' => ['organ']]);原因:因为我使用的是beyondcode/laravel-websockets,所以我深入研究了src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php,在这个文件中,retrieveUser方法将得到用户。在此文件中,如果为通道提供了一个选项,则将返回指定警卫中的用户。您可以定义一个或多个保护程序,但是它将只返回一个登录为数组中第一个保护程序的用户。
protected function retrieveUser($request, $channel)
{
$options = $this->retrieveChannelOptions($channel);
$guards = $options['guards'] ?? null;
if (is_null($guards)) {
return $request->user();
}
foreach (Arr::wrap($guards) as $guard) {
if ($user = $request->user($guard)) {
return $user;
}
}
}https://stackoverflow.com/questions/51007102
复制相似问题