Skip to content

Commit a0d8e19

Browse files
committed
Reuse HttpRequest
1 parent e7e3c73 commit a0d8e19

File tree

6 files changed

+120
-301
lines changed

6 files changed

+120
-301
lines changed

rtc/example/example.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), 'src', 'Rtc_v3.php'));
4+
require_once("../../autoload.php");
45

56
$ak = 'gwd_gV4gPKZZsmEOvAuNU1AcumicmuHooTfu64q5';
67
$sk = 'xxxx';
@@ -12,30 +13,30 @@
1213
try {
1314
//创建app
1415
$resp = $client->createApp($hub, $title, $maxUsers);
15-
print_r($resp);
16+
print_r($resp);exit;
1617
// 获取app状态
17-
$resp = $client->getApp('dg82qmmnx');
18-
print_r($resp);
18+
$resp = $client->getApp('dgbg9ym9p');
19+
print_r($resp);exit;
1920
//修改app状态
2021
$mergePublishRtmp = null;
2122
$mergePublishRtmp['enable'] = true;
22-
$resp = $client->UpdateApp('dg82qmmnx', $hub, $title, $maxUsers, $mergePublishRtmp);
23-
print_r($resp);
24-
// //删除app
25-
$resp = $client->deleteApp('dg82qmmnx');
26-
print_r($resp);
23+
$resp = $client->UpdateApp('dgbgljsxp', $hub, $title, $maxUsers, $mergePublishRtmp);
24+
print_r($resp);exit;
25+
//删除app
26+
$resp = $client->deleteApp('dgbgljsxp');
27+
print_r($resp);exit;
2728
//获取房间连麦的成员
28-
$resp=$client->getappUserNum("despz8txh", 'lfxl');
29-
print_r($resp);
29+
$resp=$client->getappUserNum("dgbfvvzid", 'lfxl');
30+
print_r($resp);exit;
3031
//剔除房间的连麦成员
31-
$resp=$client->kickingPlayer("deqq25wl9", 'lfx', "qiniu-f6e07b78-4dc8-45fb-a701-a9e158abb8e6");
32-
print_r($resp);
32+
$resp=$client->kickingPlayer("dgbfvvzid", 'lfx', "qiniu-f6e07b78-4dc8-45fb-a701-a9e158abb8e6");
33+
print_r($resp);exit;
3334
// 列举房间
34-
$resp=$client->listRooms("deqq25wl9", 'lfx', null, null);
35-
print_r($resp);
35+
$resp=$client->listRooms("dgbfvvzid", 'lfx', null, null);
36+
print_r($resp);exit;
3637
//鉴权的有效时间: 1个小时.
37-
$resp = $client->appToken("desqva26d", "lfxl", '1111', (time()+3600), 'user');
38-
print_r($resp);
38+
$resp = $client->appToken("dgbfvvzid", "lfxl", '1111', (time()+3600), 'user');
39+
print_r($resp);exit;
3940
} catch (\Exception $e) {
4041
echo "Error:", $e, "\n";
4142
}

rtc/src/QiniuRtc/AppClient.php

Lines changed: 97 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22
namespace QiniuRtc;
33

4+
use Qiniu\Zone;
5+
use Qiniu\Http\Client;
6+
use Qiniu\Http\Error;
7+
48
class AppClient
59
{
610
private $_transport;
@@ -33,12 +37,12 @@ public function createApp($hub, $title, $maxUsers = null, $noAutoKickUser = null
3337
$params['noAutoKickUser'] = $noAutoKickUser;
3438
}
3539
$body = json_encode($params);
36-
try {
37-
$ret = $this->_transport->send(HttpRequest::POST, $this->_baseURL, $body);
38-
} catch (\Exception $e) {
39-
return $e;
40+
list($ret, $err) = $this->post("POST", $this->_baseURL, $body);
41+
if ($err !== null) {
42+
return $err;
43+
} else {
44+
return $ret;
4045
}
41-
return $ret;
4246
}
4347

4448
/*
@@ -71,12 +75,12 @@ public function updateApp($appId, $hub, $title, $maxUsers = null, $mergePublishR
7175
$params['mergePublishRtmp'] = $mergePublishRtmp;
7276
}
7377
$body = json_encode($params);
74-
try {
75-
$ret = $this->_transport->send(HttpRequest::POST, $url, $body);
76-
} catch (\Exception $e) {
77-
return $e;
78+
list($ret, $err) = $this->post("POST", $url, $body);
79+
if ($err !== null) {
80+
return $err;
81+
} else {
82+
return $ret;
7883
}
79-
return $ret;
8084
}
8185

8286
/*
@@ -85,12 +89,12 @@ public function updateApp($appId, $hub, $title, $maxUsers = null, $mergePublishR
8589
public function getApp($appId)
8690
{
8791
$url = $this->_baseURL . '/' . $appId;
88-
try {
89-
$ret = $this->_transport->send(HttpRequest::GET, $url);
90-
} catch (\Exception $e) {
91-
return $e;
92+
list($ret, $err) = $this->get("GET", $url);
93+
if ($err !== null) {
94+
return $err;
95+
} else {
96+
return $ret;
9297
}
93-
return $ret;
9498
}
9599

96100
/*
@@ -99,12 +103,12 @@ public function getApp($appId)
99103
public function deleteApp($appId)
100104
{
101105
$url = $this->_baseURL . '/' . $appId;
102-
try {
103-
$ret = $this->_transport->send(HttpRequest::DELETE, $url);
104-
} catch (\Exception $e) {
105-
return $e;
106+
list($ret, $err) = $this->delete("DELETE", $url);
107+
if ($err !== null) {
108+
return $err;
109+
} else {
110+
return $ret;
106111
}
107-
return $ret;
108112
}
109113

110114
/*
@@ -115,12 +119,12 @@ public function deleteApp($appId)
115119
public function getappUserNum($appId, $roomName)
116120
{
117121
$url = sprintf("%s/%s/rooms/%s/users", $this->_baseURL, $appId, $roomName);
118-
try {
119-
$ret = $this->_transport->send(HttpRequest::GET, $url);
120-
} catch (\Exception $e) {
121-
return $e;
122+
list($ret, $err) = $this->get("GET", $url);
123+
if ($err !== null) {
124+
return $err;
125+
} else {
126+
return $ret;
122127
}
123-
return $ret;
124128
}
125129

126130
/*
@@ -132,12 +136,12 @@ public function getappUserNum($appId, $roomName)
132136
public function kickingPlayer($appId, $roomName, $userId)
133137
{
134138
$url = sprintf("%s/%s/rooms/%s/users/%s", $this->_baseURL, $appId, $roomName, $userId);
135-
try {
136-
$ret = $this->_transport->send(HttpRequest::DELETE, $url);
137-
} catch (\Exception $e) {
138-
return $e;
139+
list($ret, $err) = $this->delete("DELETE", $url);
140+
if ($err !== null) {
141+
return $err;
142+
} else {
143+
return $ret;
139144
}
140-
return $ret;
141145
}
142146

143147
/*
@@ -165,12 +169,12 @@ public function listRooms($appId, $prefix = null, $offset = null, $limit = null)
165169
} else {
166170
$url = sprintf("%s/%s/rooms", $this->_baseURL, $appId);
167171
}
168-
try {
169-
$ret = $this->_transport->send(HttpRequest::GET, $url);
170-
} catch (\Exception $e) {
171-
return $e;
172+
list($ret, $err) = $this->get("GET", $url);
173+
if ($err !== null) {
174+
return $err;
175+
} else {
176+
return $ret;
172177
}
173-
return $ret;
174178
}
175179

176180
/*
@@ -193,4 +197,62 @@ public function appToken($appId, $roomName, $userId, $expireAt, $permission)
193197
$encodedSign = Utils::base64UrlEncode($sign);
194198
return $this->_mac->_accessKey . ":" . $encodedSign . ":" . $encodedappAccess;
195199
}
200+
201+
private function get($method, $url)
202+
{
203+
if ($method != "GET") {
204+
$cType = 'application/json';
205+
} else {
206+
$cType = null;
207+
}
208+
$macToken = $this->_mac->MACToken($method, $url, $cType, $body);
209+
$headers = array(
210+
'Content-Type' => $cType,
211+
'Authorization' => $macToken,
212+
);
213+
$ret = Client::get($url, $headers);
214+
if (!$ret->ok()) {
215+
return array(null, new Error($url, $ret));
216+
}
217+
return array($ret->json(), null);
218+
}
219+
220+
private function delete($method, $url)
221+
{
222+
if ($method != "GET") {
223+
$cType = 'application/json';
224+
} else {
225+
$cType = null;
226+
}
227+
$macToken = $this->_mac->MACToken($method, $url, $cType, $body);
228+
$headers = array(
229+
'Content-Type' => $cType,
230+
'Authorization' => $macToken,
231+
);
232+
$ret = Client::delete($url, $headers);
233+
if (!$ret->ok()) {
234+
return array(null, new Error($url, $ret));
235+
}
236+
return array($ret->json(), null);
237+
}
238+
239+
private function post($method, $url, $body)
240+
{
241+
if ($method != "GET") {
242+
$cType = 'application/json';
243+
} else {
244+
$cType = null;
245+
}
246+
$macToken = $this->_mac->MACToken($method, $url, $cType, $body);
247+
$headers = array(
248+
'Content-Type' => $cType,
249+
'Authorization' => $macToken,
250+
);
251+
$ret = Client::post($url, $body, $headers);
252+
if (!$ret->ok()) {
253+
return array(null, new Error($url, $ret));
254+
}
255+
$r = ($ret->body === null) ? array() : $ret->json();
256+
return array($r, null);
257+
}
196258
}

0 commit comments

Comments
 (0)