-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotengine.js
83 lines (68 loc) · 1.65 KB
/
dotengine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
/*
dotEngine server SDK
*/
const net = require('net');
const request = require('request');
const jwt = require('jwt-simple');
const dotEngineUrl = 'https://janus.dot.cc/api/';
const tokenExpires = 3600*24;
class DotEngine
{
constructor(appkey,appSecret)
{
this._appkey = appkey;
this._appSecret = appSecret;
}
get appKey()
{
return this._appkey;
}
get appSecret()
{
return this._appSecret;
}
createToken(options,callback)
{
let room = options.room;
let user = options.user;
let role = '';
if(options.role){
role = options.role;
}
let expires = tokenExpires;
if(options.expires){
expires = options.expires;
}
let data = {
room:room,
user:user,
appkey:this._appkey,
expires:expires,
role:role
};
let sign = jwt.encode(data,this._appSecret);
let roptions = {
uri: dotEngineUrl + 'createToken',
method:'POST',
json: {
appkey:this._appkey,
sign:sign
}
};
request(roptions, (error,response,body) => {
if(!error && response.statusCode == 200){
let ret = body;
if(ret.s > 10000){
// error
callback(ret.e,null);
return;
}
callback(null,ret.d.token);
} else {
callback(error, null);
}
});
}
}
module.exports = DotEngine;