【问题标题】:laravel broadcasting in admin guard管理员守卫中的laravel广播
【发布时间】:2018-06-23 09:43:57
【问题描述】:

在我的 laravel 网站中,我们有两个守卫 admincustomer 这个守卫的表格是 ->customer tableadmin table

我想通过laravel broadcasting 将通知从customer 发送到admin 这是我将 .js 文件中的代码放入 admin guard

server.js 文件:

window.Echo = new Echo({

   broadcaster: 'socket.io',
   host: window.location.hostname + ':6001',
}); 

* i have a admin by id=2 *

Echo.private('App.Models.Admin.2')
    .notification((notification) => {
        console.log(notification.type);
    });

进入客户保护

\Notification::send(Admin::find(2) , new SimpleAlert('hey bro'));

SimpleAlert.php:

public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'message' => $this->message,
        'type' => 'broadcast'
    ]);
}

所以,我启动 laravel-echo-server 并打开两个谷歌浏览器选项卡,一个用于 admin guard,另一个用于 customer guard,然后登录到这两个守卫。
控制台没有错误。 customer sending notification 没有错误,但 `admin 没有收到通知。

问题:
当我进入admin guard时,laravel-echo-server 会显示这个

客户端无法通过身份验证,得到 HTTP 状态 403

请帮帮我。

【问题讨论】:

标签: php laravel socket.io broadcast


【解决方案1】:

您可以在routes/channels.php 中添加授权。试试这个

Broadcast::channel('App.Models.Admin.{userId}', function ($user, $userId) {
    return $user->id === $userId;
});

【讨论】:

    【解决方案2】:

    这个答案对我不起作用。它不起作用的原因是因为您将预期的 id ($userId) 与 user 表上的经过身份验证的用户进行比较,而不是 admin 表。要在私人频道上向管理员发送消息,这对我有用:

    在路由/channels.php 中。

    Broadcast::channel('App.Models.Admin.{userId}', function ($user, $userId) {
        return auth()->guard('admin')->user()->id === $userId;
    });
    

    【讨论】:

      猜你喜欢
      • 2018-05-12
      • 2018-12-03
      • 2019-09-30
      • 2016-04-26
      • 2021-10-22
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多