如何构造Pusher GET请求获取所有PRESENCE频道user_count?求CURL示例
Pusher PRESENCE频道user_count获取方案
可直接运行的CURL请求示例
请先将请求中的占位符替换为你Pusher应用的对应配置值:
YOUR_PUSHER_CLUSTER:Pusher应用所属集群,例如ap1、us-east-1YOUR_APP_ID:Pusher控制台获取的应用IDYOUR_APP_KEY:Pusher应用公钥CURRENT_UNIX_TIMESTAMP:当前10位Unix时间戳(与Pusher服务器时间差不能超过5分钟)GENERATED_AUTH_SIGNATURE:按下方规则生成的请求签名
curl "https://YOUR_PUSHER_CLUSTER.pusher.com/apps/YOUR_APP_ID/channels?filter_by_prefix=presence-&info=user_count&auth_key=YOUR_APP_KEY&auth_timestamp=CURRENT_UNIX_TIMESTAMP&auth_version=1.0&auth_signature=GENERATED_AUTH_SIGNATURE"
签名生成规则
签名使用HMAC-SHA256算法生成,规则如下:
- 拼接签名字符串,格式为
请求方法\n请求路径\n排序后的所有查询参数字符串,示例结构:
GET /apps/YOUR_APP_ID/channels auth_key=YOUR_APP_KEY&auth_timestamp=CURRENT_UNIX_TIMESTAMP&auth_version=1.0&filter_by_prefix=presence-&info=user_count
- 以你的Pusher应用私钥为密钥,对上述字符串做HMAC-SHA256加密,得到的十六进制结果就是
auth_signature的值
Laravel 更简便实现方案(无需手动拼请求)
由于你使用Laravel技术栈,直接使用官方Pusher SDK可跳过签名、请求构造等繁琐步骤:
- 安装官方SDK:
composer require pusher/pusher-php-server
- 确保
config/broadcasting.php中已配置好Pusher相关参数,调用示例代码:
$pusher = new Pusher\Pusher( config('broadcasting.connections.pusher.key'), config('broadcasting.connections.pusher.secret'), config('broadcasting.connections.pusher.app_id'), ['cluster' => config('broadcasting.connections.pusher.options.cluster')] ); $channelsData = $pusher->get_channels([ 'filter_by_prefix' => 'presence-', 'info' => 'user_count' ]); // $channelsData->channels 即为所有带user_count的PRESENCE频道列表
内容的提问来源于stack exchange,提问作者ArcticMediaRyan
相关产品推荐
相关产品推荐

