Skip to content

Commit 2e8f50c

Browse files
committed
feat: add rtc demo
1 parent 2fe9de0 commit 2e8f50c

File tree

9 files changed

+280
-0
lines changed

9 files changed

+280
-0
lines changed

examples/rtc/rtc_createApp.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// 绑定的直播 hub,使用此 hub 的资源进行推流等业务功能,hub 与 app 必须属于同一个七牛账户
15+
$hub = 'rtchub';
16+
17+
// app 的名称,注意:Title 不是唯一标识,重复 create 动作将生成多个 app
18+
$title = 'rtc';
19+
20+
// 连麦房间支持的最大在线人数
21+
$maxUsers = 20;
22+
23+
// 创建应用
24+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_1
25+
26+
list($ret, $err) = $client->createApp($hub, $title, $maxUsers);
27+
if ($err !== null) {
28+
var_dump($err);
29+
} else {
30+
echo "\n====> Create Successfully: \n";
31+
var_dump($ret);
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成
15+
$appId = 'xxxx';
16+
17+
// 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$
18+
$roomName = 'room01';
19+
20+
// 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$
21+
$userId = '001';
22+
23+
// 鉴权的有效时间,传入以秒为单位的64位 Unix 绝对时间,token 将在该时间后失效
24+
$expireAt = time()+3600;
25+
26+
// "admin" 或 "user",默认为 "user" 。当权限角色为 "admin" 时,拥有将其他用户移除出房间等特权
27+
$permission = 'admin';
28+
29+
// 生成加入房间的令牌 RoomToken
30+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#1
31+
32+
$RoomToken = $client->appToken($appId, $roomName, $userId, $expireAt, $permission);
33+
echo "\n====> Create RoomToken Successfully: \n";
34+
var_dump($RoomToken);

examples/rtc/rtc_deleteApp.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成
15+
$appId = 'xxxx';
16+
17+
// 删除应用
18+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_1
19+
20+
list($ret, $err) = $client->deleteApp($appId);
21+
if ($err !== null) {
22+
var_dump($err);
23+
} else {
24+
echo "\n====> Delete $appId Successfully \n";
25+
}

examples/rtc/rtc_getApp.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成
15+
$appId = 'xxxx';
16+
17+
// 获取一个应用当前的配置信息
18+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_1
19+
20+
list($ret, $err) = $client->getApp($appId);
21+
if ($err !== null) {
22+
var_dump($err);
23+
} else {
24+
echo "\n====> $appId Conf: \n";
25+
var_dump($ret);
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成
15+
$appId = 'xxxx';
16+
17+
// 房间名称
18+
$roomName = 'room01';
19+
20+
// 操作所剔除的用户
21+
$userId = '001';
22+
23+
// 指定一个用户踢出房间
24+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_2
25+
26+
list($ret, $err) = $client->kickUser($appId, $roomName, $userId);
27+
if ($err !== null) {
28+
var_dump($err);
29+
} else {
30+
echo "\n====> Kick User $userId Successfully \n";
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成
15+
$appId = 'xxxx';
16+
17+
// 所查询房间名的前缀索引,可以为空
18+
$prefix = null;
19+
20+
// 分页查询的位移标记
21+
$offset = null;
22+
23+
// 此次查询的最大长度
24+
$limit = '100';
25+
26+
// 获取当前所有活跃的房间
27+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_2
28+
29+
list($ret, $err) = $client->listActiveRooms($appId, $prefix, $offset, $limit);
30+
if ($err !== null) {
31+
var_dump($err);
32+
} else {
33+
echo "\n====> Active Rooms:\n";
34+
var_dump($ret);
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成
15+
$appId = 'xxxx';
16+
17+
// 房间名称
18+
$roomName = 'room01';
19+
20+
// 列出指定房间下当前所有用户
21+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_2
22+
23+
list($ret, $err) = $client->listUser($appId, $roomName);
24+
if ($err !== null) {
25+
var_dump($err);
26+
} else {
27+
echo "\n====> User List: \n";
28+
var_dump($ret);
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成
15+
$appId = 'xxxx';
16+
17+
// 房间名称
18+
$roomName = 'room01';
19+
20+
// 停止一个房间的合流转推
21+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_2
22+
23+
list($ret, $err) = $client->stopMerge($appId, $roomName);
24+
if ($err !== null) {
25+
var_dump($err);
26+
} else {
27+
echo "\n====> Stop Merge Successfully \n";
28+
}

examples/rtc/rtc_updateApp.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
require_once __DIR__ . '/../../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Rtc\AppClient;
6+
7+
// 控制台获取密钥:https://portal.qiniu.com/user/key
8+
$accessKey = getenv('QINIU_ACCESS_KEY');
9+
$secretKey = getenv('QINIU_SECRET_KEY');
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$client = new AppClient($auth);
13+
14+
// app 的唯一标识,创建的时候由系统生成。
15+
$appId = "xxxx";
16+
17+
// 绑定的直播 hub,使用此 hub 的资源进行推流等业务功能,hub 与 app 必须属于同一个七牛账户
18+
$hub = 'rtchub';
19+
20+
// app 的名称,注意:Title 不是唯一标识,重复 create 动作将生成多个 app
21+
$title = 'rtc';
22+
23+
// 连麦房间支持的最大在线人数
24+
$maxUsers = 30;
25+
26+
// 连麦合流转推 RTMP 的配置
27+
$mergePublishRtmp = array();
28+
$mergePublishRtmp['enable'] = true;
29+
30+
// 更新一个应用的配置信息
31+
// 注意:调用这个接口后仅对调用后新创建的房间有效,已经存在的房间需要等待被关闭重新创建后生效
32+
// 参考文档:https://doc.qnsdk.com/rtn/docs/server_overview#2_1
33+
34+
list($ret, $err) = $client->updateApp($appId, $hub, $title, $maxUsers, false, $mergePublishRtmp);
35+
if ($err !== null) {
36+
var_dump($err);
37+
} else {
38+
echo "\n====> Update $appId Conf Successfully: \n";
39+
var_dump($ret);
40+
}

0 commit comments

Comments
 (0)