forked from rickyrauch/Balloons.IO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
99 lines (79 loc) · 2.32 KB
/
Copy pathdb.js
File metadata and controls
99 lines (79 loc) · 2.32 KB
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
/*
* Module dependencies
*/
var redis = require('redis')
, debug = require('debug')('Balloons:db');
var DB = function(options) {
if(options.redistogo) {
var rtg = require('url').parse(options.redistogo.url);
this.client = redis.createClient(rtg.port, rtg.hostname);
// auth 1st part is username and 2nd is password separated by ":"
this.client.auth(rtg.auth.split(':')[1]);
} else {
this.client = redis.createClient();
}
return this;
};
DB.createConnection = function(options) {
return new DB(options);
};
module.exports = DB;
DB.prototype.removePath = function(path, fn) {
this.client.keys(path, function(err, keys){
this.del(keys, fn);
});
};
DB.prototype.users = {};
DB.prototype.sockets = {};
DB.prototype.rooms = {};
/*DB.prototype.users.remove = function(fn) {
};*/
DB.prototype.sockets.clean = function(fn) {
this.client.smembers('socketio:sockets', function(err, sockets) {
if(sockets.length) client.del(sockets);
debug('Sockets cleaned');
});
};
DB.prototype.clean = function(fn) {
debug('Cleaning db');
this.removePath('sockets:for:*', function(err){
debug("Deleted sockets reference for each user >> %s", err || "Done!");
});
this.removePath('rooms:*:users*', function(err){
debug("Deleted users reference in rooms >> %s", err || "Done!");
});
this.sockets.clean();
};
DB.prototype.user.getRooms = function(id, fn) {
this.client
.multi()
.
this.client.smembers('rooms:public', function(err, rooms){
});
};
client.smembers('balloons:public:rooms', function(err, public_rooms) {
var rooms = []
, len = public_rooms.length;
if(!len) fn([]);
public_rooms.sort(caseInsensitiveSort);
public_rooms.forEach(function(room_key, index) {
client.hgetall('rooms:' + room_key + ':info', function(err, room) {
// prevent for a room info deleted before this check
if(!err && room && Object.keys(room).length) {
// add room info
rooms.push({
key: room.key || room.name, // temp
name: room.name,
online: room.online || 0
});
// check if last room
if(rooms.length == len) fn(null, rooms);
} else {
if(err) return fn(err, null);
// reduce check length
len -= 1;
}
});
});
});
};