1

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

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

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

我以这种方式在频道上广播:

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

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

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

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

有没有其他方法可以将用户注册到他的频道?

先感谢您。

4

1 回答 1

2

您需要在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)
于 2018-01-23T04:34:05.140 回答