-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.js
More file actions
executable file
·77 lines (67 loc) · 2.14 KB
/
push.js
File metadata and controls
executable file
·77 lines (67 loc) · 2.14 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
/**
* @author
* @copyright
*/
var g = require('./global.js');
var self;
module.exports = {
WS_PUSH: 'push',
_cacheKey: function (clientId) {
return 'push_' + clientId;
},
_deliver: function (clientId, msg) {
if (g.isconnected(clientId)) {
console.log('Push - {0}, {1}'.format(clientId, msg));
g.io.sockets.in(String(clientId)).emit(self.WS_PUSH, msg);
return true;
}
return false;
/*
var res = false;
var socs = g.getSockets(clientId);
for (var i = 0; i < socs.length; i++) {
var soc = socs[i];
if (soc.connected && soc.ready) {
soc.emit(self.WS_PUSH, msg);
res = true;
} else {
g.error('Error - ' + soc.name + ' not connected or ready');
}
}
return res;
*/
},
flush: function (clientId) {
// peek to see if there is a pending notification
// redis.LRANGE() returns an array
g['redis'].LRANGE(self._cacheKey(clientId), 0, 0, function (err, ret) {
if (err != null) {
g.error(err);
} else if (!g.empty(ret) && self._deliver(clientId, ret[0])) {
g['redis'].LPOP(self._cacheKey(clientId), function (err, ret) {
if (err != null) {
g.error(err);
} else {
self.flush(clientId);
}
});
}
});
},
send: function (clientId, msg, queue) {
//console.log("clientid: " + clientId + " msg: " + msg);
// deliver immediately if the user is online, otherwise queue in redis
if (self._deliver(clientId, msg)) {
self.flush(clientId);
} else if (queue) {
g['redis'].RPUSH(self._cacheKey(clientId), msg, function (err, ret) {
if (err != null) {
g.error('Error - ' + err);
} else {
//self.flush(clientId);
}
});
}
},
}
self = module.exports