1 | <?php |
||||
2 | |||||
3 | namespace Qiniu\Rtc; |
||||
4 | |||||
5 | use Qiniu\Auth; |
||||
6 | use Qiniu\Config; |
||||
7 | use Qiniu\Http\Error; |
||||
8 | use Qiniu\Http\Client; |
||||
9 | use Qiniu\Http\Proxy; |
||||
10 | |||||
11 | class AppClient |
||||
12 | { |
||||
13 | private $auth; |
||||
14 | private $baseURL; |
||||
15 | private $proxy; |
||||
16 | |||||
17 | public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null) |
||||
18 | { |
||||
19 | $this->auth = $auth; |
||||
20 | $this->baseURL = sprintf("%s/%s/apps", Config::RTCAPI_HOST, Config::RTCAPI_VERSION); |
||||
21 | $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password); |
||||
22 | } |
||||
23 | |||||
24 | /** |
||||
25 | * 创建应用 |
||||
26 | * |
||||
27 | * @param string $hub 绑定的直播 hub |
||||
28 | * @param string $title app 的名称 注意,Title 不是唯一标识,重复 create 动作将生成多个 app |
||||
29 | * @param int $maxUsers 连麦房间支持的最大在线人数 |
||||
30 | * @param bool $noAutoKickUser 禁止自动踢人(抢流),默认为 false |
||||
31 | * @return array |
||||
32 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1 |
||||
33 | */ |
||||
34 | public function createApp($hub, $title, $maxUsers = null, $noAutoKickUser = null) |
||||
35 | { |
||||
36 | $params = array(); |
||||
37 | $params['hub'] = $hub; |
||||
38 | $params['title'] = $title; |
||||
39 | if (!empty($maxUsers)) { |
||||
40 | $params['maxUsers'] = $maxUsers; |
||||
41 | } |
||||
42 | if ($noAutoKickUser !== null) { |
||||
43 | $params['noAutoKickUser'] = $noAutoKickUser; |
||||
44 | } |
||||
45 | $body = json_encode($params); |
||||
46 | return $this->post($this->baseURL, $body); |
||||
47 | } |
||||
48 | |||||
49 | /** |
||||
50 | * 更新一个应用的配置信息 |
||||
51 | * |
||||
52 | * @param string $appId app 的唯一标识,创建的时候由系统生成 |
||||
53 | * @param string $hub app 的名称,可选 |
||||
54 | * @param string $title 绑定的直播 hub,可选,用于合流后 rtmp 推流 |
||||
55 | * @param int $maxUsers 连麦房间支持的最大在线人数,可选 |
||||
56 | * @param bool $noAutoKickUser 禁止自动踢人,可选 |
||||
57 | * @param null $mergePublishRtmp 连麦合流转推 RTMP 的配置,可选择。其详细配置可以参考文档 |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
58 | * @return array |
||||
59 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1 |
||||
60 | */ |
||||
61 | public function updateApp($appId, $hub, $title, $maxUsers = null, $noAutoKickUser = null, $mergePublishRtmp = null) |
||||
62 | { |
||||
63 | $url = $this->baseURL . '/' . $appId; |
||||
64 | $params = array(); |
||||
65 | $params['hub'] = $hub; |
||||
66 | $params['title'] = $title; |
||||
67 | if (!empty($maxUsers)) { |
||||
68 | $params['maxUsers'] = $maxUsers; |
||||
69 | } |
||||
70 | if ($noAutoKickUser !== null) { |
||||
71 | $params['noAutoKickUser'] = $noAutoKickUser; |
||||
72 | } |
||||
73 | if (!empty($mergePublishRtmp)) { |
||||
74 | $params['mergePublishRtmp'] = $mergePublishRtmp; |
||||
75 | } |
||||
76 | $body = json_encode($params); |
||||
77 | return $this->post($url, $body); |
||||
78 | } |
||||
79 | |||||
80 | /** |
||||
81 | * 获取应用信息 |
||||
82 | * |
||||
83 | * @param string $appId |
||||
84 | * @return array |
||||
85 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1 |
||||
86 | */ |
||||
87 | public function getApp($appId) |
||||
88 | { |
||||
89 | $url = $this->baseURL . '/' . $appId; |
||||
90 | return $this->get($url); |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * 删除应用 |
||||
95 | * |
||||
96 | * @param string $appId app 的唯一标识,创建的时候由系统生成 |
||||
97 | * @return array |
||||
98 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1 |
||||
99 | */ |
||||
100 | public function deleteApp($appId) |
||||
101 | { |
||||
102 | $url = $this->baseURL . '/' . $appId; |
||||
103 | return $this->delete($url); |
||||
104 | } |
||||
105 | |||||
106 | /** |
||||
107 | * 获取房间内用户列表 |
||||
108 | * |
||||
109 | * @param string $appId app 的唯一标识,创建的时候由系统生成 |
||||
110 | * @param string $roomName 操作所查询的连麦房间 |
||||
111 | * @return array |
||||
112 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2 |
||||
113 | */ |
||||
114 | public function listUser($appId, $roomName) |
||||
115 | { |
||||
116 | $url = sprintf("%s/%s/rooms/%s/users", $this->baseURL, $appId, $roomName); |
||||
117 | return $this->get($url); |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * 指定一个用户踢出房间 |
||||
122 | * |
||||
123 | * @param string $appId app 的唯一标识,创建的时候由系统生成 |
||||
124 | * @param string $roomName 连麦房间 |
||||
125 | * @param string $userId 操作所剔除的用户 |
||||
126 | * @return mixed |
||||
127 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2 |
||||
128 | */ |
||||
129 | public function kickUser($appId, $roomName, $userId) |
||||
130 | { |
||||
131 | $url = sprintf("%s/%s/rooms/%s/users/%s", $this->baseURL, $appId, $roomName, $userId); |
||||
132 | return $this->delete($url); |
||||
133 | } |
||||
134 | |||||
135 | /** |
||||
136 | * 停止一个房间的合流转推 |
||||
137 | * |
||||
138 | * @param string $appId |
||||
139 | * @param string $roomName |
||||
140 | * @return array |
||||
141 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2 |
||||
142 | */ |
||||
143 | public function stopMerge($appId, $roomName) |
||||
144 | { |
||||
145 | $url = sprintf("%s/%s/rooms/%s/merge", $this->baseURL, $appId, $roomName); |
||||
146 | return $this->delete($url); |
||||
147 | } |
||||
148 | |||||
149 | /** |
||||
150 | * 获取应用中活跃房间 |
||||
151 | * |
||||
152 | * @param string $appId 连麦房间所属的 app |
||||
153 | * @param null $prefix 所查询房间名的前缀索引,可以为空。 |
||||
0 ignored issues
–
show
|
|||||
154 | * @param int $offset 分页查询的位移标记 |
||||
155 | * @param int $limit 此次查询的最大长度 |
||||
156 | * @return array |
||||
157 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2 |
||||
158 | */ |
||||
159 | public function listActiveRooms($appId, $prefix = null, $offset = null, $limit = null) |
||||
160 | { |
||||
161 | $query = array(); |
||||
162 | if (isset($prefix)) { |
||||
163 | $query['prefix'] = $prefix; |
||||
164 | } |
||||
165 | if (isset($offset)) { |
||||
166 | $query['offset'] = $offset; |
||||
167 | } |
||||
168 | if (isset($limit)) { |
||||
169 | $query['limit'] = $limit; |
||||
170 | } |
||||
171 | if (isset($query) && !empty($query)) { |
||||
172 | $query = '?' . http_build_query($query); |
||||
173 | $url = sprintf("%s/%s/rooms%s", $this->baseURL, $appId, $query); |
||||
174 | } else { |
||||
175 | $url = sprintf("%s/%s/rooms", $this->baseURL, $appId); |
||||
176 | } |
||||
177 | return $this->get($url); |
||||
178 | } |
||||
179 | |||||
180 | /** |
||||
181 | * 生成加入房间的令牌 |
||||
182 | * |
||||
183 | * @param string $appId app 的唯一标识,创建的时候由系统生成 |
||||
184 | * @param string $roomName 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$ |
||||
185 | * @param string $userId 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$ |
||||
186 | * @param int $expireAt 鉴权的有效时间,传入以秒为单位的64位 Unix 绝对时间 |
||||
187 | * @param string $permission 该用户的房间管理权限,"admin" 或 "user",默认为 "user" |
||||
188 | * @return string |
||||
189 | * @link https://doc.qnsdk.com/rtn/docs/server_overview#1 |
||||
190 | */ |
||||
191 | public function appToken($appId, $roomName, $userId, $expireAt, $permission) |
||||
192 | { |
||||
193 | $params = array(); |
||||
194 | $params['appId'] = $appId; |
||||
195 | $params['userId'] = $userId; |
||||
196 | $params['roomName'] = $roomName; |
||||
197 | $params['permission'] = $permission; |
||||
198 | $params['expireAt'] = $expireAt; |
||||
199 | $appAccessString = json_encode($params); |
||||
200 | return $this->auth->signWithData($appAccessString); |
||||
201 | } |
||||
202 | |||||
203 | private function get($url, $cType = null) |
||||
204 | { |
||||
205 | $rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType); |
||||
206 | $rtcToken['Content-Type'] = $cType; |
||||
207 | $ret = Client::get($url, $rtcToken, $this->proxy->makeReqOpt()); |
||||
208 | if (!$ret->ok()) { |
||||
209 | return array(null, new Error($url, $ret)); |
||||
210 | } |
||||
211 | return array($ret->json(), null); |
||||
212 | } |
||||
213 | |||||
214 | private function delete($url, $contentType = 'application/json') |
||||
215 | { |
||||
216 | $rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType); |
||||
217 | $rtcToken['Content-Type'] = $contentType; |
||||
218 | $ret = Client::delete($url, $rtcToken, $this->proxy->makeReqOpt()); |
||||
0 ignored issues
–
show
$this->proxy->makeReqOpt() of type Qiniu\Http\RequestOptions is incompatible with the type array expected by parameter $opt of Qiniu\Http\Client::delete() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
219 | if (!$ret->ok()) { |
||||
220 | return array(null, new Error($url, $ret)); |
||||
221 | } |
||||
222 | return array($ret->json(), null); |
||||
223 | } |
||||
224 | |||||
225 | private function post($url, $body, $contentType = 'application/json') |
||||
226 | { |
||||
227 | $rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType); |
||||
228 | $rtcToken['Content-Type'] = $contentType; |
||||
229 | $ret = Client::post($url, $body, $rtcToken, $this->proxy->makeReqOpt()); |
||||
230 | if (!$ret->ok()) { |
||||
231 | return array(null, new Error($url, $ret)); |
||||
232 | } |
||||
233 | $r = ($ret->body === null) ? array() : $ret->json(); |
||||
234 | return array($r, null); |
||||
235 | } |
||||
236 | } |
||||
237 |