【问题标题】:Laravel Pusher: Get all users connected to Presence ChannelLaravel Pusher:让所有用户连接到 Presence Channel
【发布时间】:2021-02-11 23:26:24
【问题描述】:

有谁知道如何从 Presence 频道获取所有连接的用户?我需要它们在我的 API 控制器中并将它们全部保存在数据库中。

我发现有些人使用Pusher::get(),但后来我收到错误消息Non-static method Pusher\Pusher::get() should not be called statically。 我看到其他人使用$this->pusher('/channels'),但是我从哪里获得 $pusher 实例?

抱歉,可能是菜鸟问题?

【问题讨论】:

  • Non-static method Pusher\Pusher::get() should not be called statically 您是否尝试将Pusher::get() 替换为Pusher->get()
  • @ThanLUONG 这给了我unexpected '->'。我想我需要$this->pusher,但不知道如何在我的控制器中获取推送实例。或者,也许这根本就不是办法?
  • 能否分享您的代码?
  • 这就是问题所在,还没有代码。我刚刚安装了 Pusher,有 1 个 Presence 频道,连接的用户可以收听,每个连接的人都可以在前端检索其他连接的用户(通过 laravel-echo)。其中一个连接的用户向控制器发送 API 请求以“开始游戏”。但是在这个控制器中,我需要与发送 api 请求的用户连接到同一频道的所有播放器。
  • 你可以试试这个链接:github.com/beyondcode/laravel-websockets/issues/71。您使用所需信息创建对象Pusher\Pusher。然后,你就得到了你想要的频道。

标签: laravel pusher laravel-echo


【解决方案1】:

$pusher 是实际的 Pusher SDK 实例,您必须自己实例化它:

$connection = config('broadcasting.connections.pusher');
$pusher = new Pusher(
    $connection['key'],
    $connection['secret'],
    $connection['app_id'],
    $connection['options'] ?? []
);

// Example 1: get all active channels
$channels = $pusher->get_channels();

// Example 2: Get all the connected users for a specific channel
$users = $pusher->get('/channels/<channel-name>/users');

顺便说一句,如果你看 here,这也是 Laravel 在内部的做法。

【讨论】:

    猜你喜欢
    • 2016-08-26
    • 2020-06-10
    • 2021-06-07
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2023-03-11
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多