Skip to content

Commit 568db97

Browse files
authored
Merge pull request #295 from GuGaobai1994/master
add rtc function
2 parents 7384b60 + fab2808 commit 568db97

File tree

7 files changed

+437
-3
lines changed

7 files changed

+437
-3
lines changed

examples/rtc_demo.js

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

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ module.exports = {
44
auth: {
55
digest: require(libPath + '/auth' + '/digest.js')
66
},
7-
cdn: require(libPath + "/cdn.js"),
7+
cdn: require(libPath + '/cdn.js'),
88
form_up: require(libPath + '/storage/form.js'),
99
resume_up: require(libPath + '/storage/resume.js'),
1010
rs: require(libPath + '/storage/rs.js'),
1111
fop: require(libPath + '/fop.js'),
1212
conf: require(libPath + '/conf.js'),
1313
rpc: require(libPath + '/rpc.js'),
1414
util: require(libPath + '/util.js'),
15-
zone: require(libPath + '/zone.js')
16-
};
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')
19+
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666
"devDependencies": {
6767
"@types/node": "^8.0.3",
6868
"blanket": "*",
69+
"eslint": "^5.0.0-alpha.2",
70+
"eslint-config-standard": "^12.0.0-alpha.0",
71+
"eslint-plugin-import": "^2.11.0",
72+
"eslint-plugin-node": "^6.0.1",
73+
"eslint-plugin-promise": "^3.7.0",
74+
"eslint-plugin-standard": "^3.1.0",
6975
"mocha": "*",
7076
"pedding": "*",
7177
"should": "1.2.2",

qiniu/rtc/app.js

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

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)