【问题标题】:Laravel Echo in multiple channels多个通道中的 Laravel Echo
【发布时间】:2018-07-01 18:23:27
【问题描述】:

我有一个问题,但没有找到令我满意的答案。

我有一个足球队的应用程序:

  • 一个用户可以加入多个团队,
  • 我想在团队中发生动作时发送通知(例如,安排比赛时)

我是这样在频道上广播的:

new PrivateChannel('team.' . $this->team->id);

但我不知道如何在多个渠道注册用户。

问题是,我需要将用户注册到他所属的每个团队的频道吗?

for ($user->teams as $team) {
  Echo.private('team.'.$team->id) 
}

还有其他方法可以让用户注册到他的频道吗?

提前谢谢你。

【问题讨论】:

    标签: laravel broadcast laravel-echo laravel-broadcast


    【解决方案1】:

    您需要在channels.php 路由文件中对每个团队频道的用户进行身份验证:

    // loop teams to create channel names like teams.team1.1, teams.team2.1
    // {id} is the user's primary key
    Broadcast::channel("teams.{$team->name}.{id}", function (Authenticatable $user, $id) {
        return (int)$user->getAuthIdentifier() === (int)$id;
    });
    
    // have Echo listen for broadcast events on the same channels. 
    Echo.private('teams.team1.1')
        .listen('SomeTeamEvent', callback)
        .listen('AnotherTeamEvent', callback2)
        ...
        .listen('LastTeamEvent', callbackX)
    ...
    Echo.private('teams.team2.1')
        .listen('SomeTeamEvent', anotherCallback)
        .listen('AnotherTeamEvent', anotherCallback2)
        ...
        .listen('LastTeamEvent', anotherCallback2)
    

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 2021-03-09
      • 2020-07-23
      • 2020-12-21
      • 2019-02-08
      相关资源
      最近更新 更多