-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheasemob.js
107 lines (93 loc) · 2.76 KB
/
easemob.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var request = require('request');
var async = require('async');
var redis = require('redis');
var _ = require('underscore');
var tseuper = function(opts, callback) {
request(opts, function(err, res, body) {
if (err) { return callback(err);}
opts.headers = _.extend(opts.headers || {}, {'Content-Type': 'application/json'});
if (!res || !body || res.statusCode !== 200) {
callback('wrong status code');
}
return callback(null, body);
});
};
function Em (opts) {
this.apiUrl = 'https://a1.easemob.com/';
this.appKey = opts.appKey;
this.clientSecret = opts.clientSecret;
this.clientId = opts.clientId;
this.orgName = opts.orgName;
this.appName = opts.appName;
this.url = this.apiUrl + this.orgName + '/' + this.appName;
if (opts.redis) {
this.redisConf = _.extend(opts.redis || {}, {host: "localhost", port: 6379});
this.emRedis = redis.createClient(this.redisConf.port, this.redisConf.host);
}
else {
this.loadToken=opts.loadToken;
this.saveToken=opts.saveToken;
}
}
Em.prototype.getToken = function(callback) {
var key = 'easemob:app:token';
var ctx = {};
var self = this;
async.waterfall([
function(next) {
if (self.emRedis) self.emRedis.get(key, next);
else self.loadToken(next);
},
function(token, next) {
if (token) return callback(null, token);
var opts = {
url: self.url + '/token',
json: {
grant_type: 'client_credentials',
client_id: self.clientId,
client_secret: self.clientSecret
},
method: 'POST'
};
tseuper(opts, function(err, body) {
if (err) return callback(err);
var token = body.access_token;
var expiredIn = body.expires_in;
ctx.token = token;
if (!token || !expiredIn) return next('get token err');
if (self.emRedis) self.emRedis.multi().set(key, token).expire(key, expiredIn - 600).exec(next);
else self.saveToken(token, next);
});
}
], function(err) {
callback(err, ctx.token);
});
};
Em.prototype.createUser = function(user, callback) {
var url = this.url + '/' + 'users';
var opts = {
url: url,
json: {
username: user.username,
password: user.password,
nickname: user.nickname
},
method: 'POST'
};
var self = this;
async.waterfall([
function(next) {
self.getToken(next);
},
function(token, next) {
opts.headers = {Authorization: 'Bearer ' + token};
tseuper(opts, next);
}
], function(err, body) {
var emUser = body && body.entities && body.entities[0] || null;
if (err || !emUser || !emUser.uuid) return callback(err || 'create chat user err');
emUser.password = user.password;
return callback(null, emUser);
});
};
module.exports = Em;