Skip to content

Commit 1d4a9ec

Browse files
author
guhao
committed
add qiniuRtc
1 parent 7384b60 commit 1d4a9ec

File tree

6 files changed

+436
-1
lines changed

6 files changed

+436
-1
lines changed

examples/rtc_demo.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const qiniu = require("../index.js");
2+
3+
//ak, sk 获取参考 https://developer.qiniu.com/dora/kb/3702/QiniuToken
4+
var ACCESS_KEY = 'DXFtikq1YuDT_WMUntOpzpWPm2UZVtEnYvN3-CUD';
5+
var SECRET_KEY = 'F397hzMohpORVZ-bBbb-IVbpdWlI4SWu8sWq78v3';
6+
var credentials = new qiniu.credentials(ACCESS_KEY, SECRET_KEY)
7+
8+
9+
//参考 https://github.com/pili-engineering/QNRTC-Server/blob/master/docs/api.md
10+
11+
var data = {
12+
'hub': 'hub',
13+
'title': 'title',
14+
'maxUsers': 10,
15+
'noAutoCloesRoom': true,
16+
'noAutoCreateRoom': true,
17+
'noAutoKickUser' : true
18+
}
19+
20+
qiniu.app.createApp(data, credentials, function (err, res) {
21+
if(err){
22+
console.log(err)
23+
} else {
24+
console.log(res)
25+
}
26+
})
27+
28+
qiniu.app.getApp('appId', credentials, function (err, res) {
29+
if(err){
30+
console.log(err)
31+
} else {
32+
console.log(res)
33+
}
34+
})
35+
36+
qiniu.app.deleteApp('appId', credentials, function (err, res) {
37+
if(err){
38+
console.log(err)
39+
} else {
40+
console.log(res)
41+
}
42+
})
43+
44+
var data1 = {
45+
'hub': 'hub',
46+
'title': 'title',
47+
'maxUsers': 10,
48+
'noAutoCloseRoom': true,
49+
'noAutoCreateRoom': true,
50+
'noAutoKickUser' : true,
51+
'mergePublishRtmp':{
52+
'enable': true,
53+
'audioOnly': true,
54+
'height': 1920,
55+
'width': 1080,
56+
'fps': 60,
57+
'kbps': 1000,
58+
'url': 'rtmp://xxx.example.com/test',
59+
'streamTitle': 'meeting'
60+
}
61+
}
62+
63+
64+
qiniu.app.updateApp('appId', data1, credentials, function (err, res) {
65+
if(err){
66+
console.log(err)
67+
} else {
68+
console.log(res)
69+
}
70+
})
71+
72+
73+
qiniu.room.listUser('appId', 'roomName', credentials, function (err, res) {
74+
if(err){
75+
console.log(err)
76+
} else {
77+
console.log(res)
78+
}
79+
})
80+
81+
qiniu.room.kickUser('appId', 'roomName', 'userId',credentials, function (err, res) {
82+
if(err){
83+
console.log(err)
84+
} else {
85+
console.log(res)
86+
}
87+
})
88+
89+
//type of(offset limit) = Num such as 5 10
90+
qiniu.room.listActiveRoom('appId', 'prefix', 'offset', 'limit' ,credentials, function (err, res) {
91+
if(err){
92+
console.log(err)
93+
} else {
94+
console.log(res)
95+
}
96+
})
97+
98+
99+
//expireAt = 1524128577 or empty
100+
var roomAccess = {
101+
'appId' : 'appId',
102+
'roomName' : 'roomName',
103+
'userId' : 'userId',
104+
'expireAt' : 1524128577,
105+
'permission' : 'admin'
106+
}
107+
108+
console.log(qiniu.room.roomToken(roomAccess, credentials))

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ module.exports = {
1212
conf: require(libPath + '/conf.js'),
1313
rpc: require(libPath + '/rpc.js'),
1414
util: require(libPath + '/util.js'),
15-
zone: require(libPath + '/zone.js')
15+
zone: require(libPath + '/zone.js'),
16+
app: require(libPath + '/rtc/app.js'),
17+
room: require(libPath + '/rtc/room.js'),
18+
credentials: require(libPath + '/rtc/credentials.js')
1619
};

qiniu/rtc/app.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
var http = require('http')
2+
3+
const host = 'rtc.qiniuapi.com'
4+
const headers = {
5+
'Content-Type': 'application/json',
6+
};
7+
8+
exports.createApp = function(app, credentials ,fn) {
9+
var options = {
10+
host: host,
11+
port: 80,
12+
path: '/v3/apps',
13+
method: 'POST',
14+
headers: headers
15+
};
16+
post(credentials, options, app, fn)
17+
}
18+
19+
exports.getApp = function (appId, credentials, fn) {
20+
var options = {
21+
host: host,
22+
port: 80,
23+
path: '/v3/apps/' + appId,
24+
method: 'GET',
25+
headers: headers
26+
};
27+
get(credentials, options, fn)
28+
}
29+
30+
exports.deleteApp = function (appId, credentials, fn) {
31+
var options = {
32+
host: host,
33+
port: 80,
34+
path: '/v3/apps/' + appId,
35+
method: 'DELETE',
36+
headers: headers
37+
};
38+
get(credentials, options, fn)
39+
}
40+
41+
exports.updateApp = function (appId, app, credentials, fn) {
42+
var options = {
43+
host: host,
44+
port: 80,
45+
path: '/v3/apps/' + appId,
46+
method: 'POST',
47+
headers: headers
48+
};
49+
post(credentials, options, app, fn)
50+
}
51+
52+
function get(credentials, options, fn){
53+
options.headers['Authorization'] = credentials.generateAccessToken(options, null);
54+
55+
var req = http.request(options, function(res) {
56+
res.setEncoding('utf-8');
57+
58+
var responseString = '';
59+
60+
res.on('data', function(data) {
61+
responseString += data;
62+
});
63+
64+
res.on('end', function() {
65+
//var resultObject = JSON.parse(responseString);
66+
67+
if (res.statusCode != 200) {
68+
var result = {
69+
code: res.statusCode,
70+
message: res.statusMessage
71+
}
72+
fn(result, null);
73+
} else {
74+
result = {
75+
code: res.statusCode,
76+
message: res.statusMessage
77+
}
78+
fn(null, result);
79+
}
80+
});
81+
});
82+
83+
req.on('error', function(e) {
84+
fn(e, null);
85+
});
86+
87+
req.end();
88+
}
89+
90+
91+
function post(credentials, options, data, fn) {
92+
var dataString = JSON.stringify(data);
93+
94+
options.headers['Authorization'] = credentials.generateAccessToken(options, dataString);
95+
96+
var req = http.request(options, function(res) {
97+
res.setEncoding('utf-8');
98+
99+
var responseString = '';
100+
101+
res.on('data', function(data) {
102+
responseString += data;
103+
});
104+
105+
res.on('end', function() {
106+
var resultObject = JSON.parse(responseString);
107+
108+
if (res.statusCode != 200) {
109+
var result = {
110+
code: res.statusCode,
111+
message: res.statusMessage
112+
}
113+
fn(result, null);
114+
} else {
115+
fn(null, resultObject);
116+
}
117+
});
118+
});
119+
req.on('error', function(e) {
120+
fn(e, null);
121+
});
122+
123+
req.write(dataString);
124+
125+
req.end();
126+
};

qiniu/rtc/credentials.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var util = require('./util');
2+
3+
function Credentials(accessKey, secretKey) {
4+
this.accessKey = accessKey;
5+
this.secretKey = secretKey;
6+
}
7+
8+
Credentials.prototype.generateAccessToken = function(options, data) {
9+
var sign = this._signRequest(options, data);
10+
var token = 'Qiniu' + ' ' + this.accessKey + ':' + sign;
11+
12+
return token;
13+
}
14+
15+
Credentials.prototype._signRequest = function(options, body) {
16+
var contentType = options.headers['Content-Type'];
17+
18+
var host = options.host;
19+
if (options.port && options.port != 80) {
20+
host = host + ':' + options.port;
21+
}
22+
23+
var data = options.method + ' ' + options.path;
24+
data += '\nHost: ' + host;
25+
if (contentType) {
26+
data += '\nContent-Type: ' + contentType;
27+
}
28+
data += '\n\n';
29+
30+
if (body && contentType && contentType != 'application/octet-stream') {
31+
data += body;
32+
}
33+
34+
35+
var digest = util.hmacSha1(data, this.secretKey);
36+
37+
38+
var sageDigest = util.base64ToUrlSafe(digest);
39+
40+
return sageDigest;
41+
}
42+
43+
Credentials.prototype.sign = function(data) {
44+
var digest = util.hmacSha1(data, this.secretKey);
45+
var sageDigest = util.base64ToUrlSafe(digest);
46+
return this.accessKey + ":" + sageDigest;
47+
48+
}
49+
50+
Credentials.prototype.signJson = function(opt) {
51+
52+
var str = JSON.stringify(opt);
53+
var encodedStr = util.urlsafeBase64Encode(str);
54+
var sign = util.hmacSha1(encodedStr, this.secretKey);
55+
var encodedSign = util.base64ToUrlSafe(sign);
56+
57+
var token = this.accessKey + ':' + encodedSign + ':' + encodedStr;
58+
return token;
59+
}
60+
61+
module.exports = exports = Credentials;

0 commit comments

Comments
 (0)