编程语言
首页 > 编程语言> > php – Laravel广播频道 – 检查连接

php – Laravel广播频道 – 检查连接

作者:互联网

我在文档或搜索中找不到这个,也许有人有一些提示.我正在尝试检查后端的状态通道上有多少个连接.

我可以像Echo那样在前端检查正常:

Echo.join('chat')
    .here((users) => {
        // users.length is the proper count of connections
    })

但有没有办法可以获得相同数量的连接,但是在Laravel内部的后端代码中?

解决方法:

如果您使用Pusher,后端可以执行以下操作:

$response = $pusher->get( '/channels/presence-channel-name/users' );
if( $response[ 'status'] == 200 ) {
  // convert to associative array for easier consumption
  $users = json_decode( $response[ 'body' ], true )[ 'users' ];
}

$userCount = count($users);

您可以在推动器documentation中阅读更多相关信息.pusher-http-php sdk也有一些相关文档.

A list of users present on a presence channel can be retrieved by
querying the /channels/[channel_name]/users resource where the
channel_name is replaced with a valid presence channel name.

这仅适用于在线渠道.

此外,您可以通过webhooks跟踪渠道中的用户.

Notify your application whenever a user subscribes to or unsubscribes
from a Presence channel.
For example, this allows you to synchronise channel presence state on
your server as well as all your application clients.

Pusher将使用以下格式的信息访问您的服务器:

{
  "name": "member_added", // or "member_removed"
  "channel": "presence-your_channel_name",
  "user_id": "a_user_id"
}

此数据可能存储在数据库的表中,也可能存储在redis中.

标签:php,laravel,broadcast,laravel-echo
来源: https://codeday.me/bug/20190608/1196976.html