redis.js 3.12 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
'use strict';

mntmn's avatar
mntmn committed
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
var notRedis = {
  state: {},
  topics: {},
  
  publish: function(topic, msg, cb) {
    //console.log("[notredis] publish",topic,msg);
    if (!this.topics[topic]) {
      this.topics[topic] = {
        subscribers: []
      };
    }
    var t=this.topics[topic];
    for (var i=0; i<t.subscribers.length; i++) {
      var s=t.subscribers[i];
      if (s.handler) {
        s.handler(topic, msg);
      }
    }
    if (cb) cb(null);
  },

  subscribe: function(topics, cb) {
    var handle = {
      handler: null,
      on: function(evt, cb) {
        if (evt == "message") {
          this.handler = cb;
        }
      }
    };
    
    for (var i=0; i<topics.length; i++) {
      var topic = topics[i];
      if (!this.topics[topic]) {
        this.topics[topic] = {
          subscribers: []
        };
      }
    
      var t=this.topics[topic];
      t.subscribers.push(handle);
    }

    cb(null, handle, topics.length);
    return handle;
  },

  get: function(key, cb) {
    cb(null, this.state[key]);
    return this.state[key];
  },

  set: function(key, val, cb) {
    this.state[key] = val;
    cb();
  },

  del: function(key, cb) {
    delete this.state[key];
    cb(null);
  },

  sadd: function(key, skey, cb) {
    if (!this.state[key]) this.state[key] = {};
    this.state[key][skey] = true;
    cb(null);
  },

  srem: function(key, skey, cb) {
    if (this.state[key]) {
      delete this.state[key][skey];
    }
    cb(null);
  },

  smembers: function(key, cb) {
    cb(null, Object.keys(this.state[key]));
  },

  incr: function(key, cb) {
    if (!this.state[key]) this.state[key] = 0;
    this.state[key]++;
    cb();
  },

  expire: function() {
  },
}
mntmn's avatar
mntmn committed
91
92

module.exports = {
mntmn's avatar
mntmn committed
93
94
  connectRedis: function() {
    this.connection = notRedis;
mntmn's avatar
mntmn committed
95
  },
mntmn's avatar
mntmn committed
96
97
98
99
100
  getConnection: function() {
    this.connectRedis();
    return this.connection;
  },
  sendMessage: function(action, model, attributes, channelId) {
mntmn's avatar
mntmn committed
101
102
103
104
105
106
107
108
    const data = JSON.stringify({
      channel_id: channelId,
      action: action,
      model: model,
      object: attributes
    });
    this.connection.publish('updates', data);
  },
mntmn's avatar
mntmn committed
109
  logIp: function(ip, cb) {
mntmn's avatar
mntmn committed
110
111
112
113
    this.connection.incr("ip_"+ ip, (err, socketCounter) => {
      cb();
    });
  },
mntmn's avatar
mntmn committed
114
  rateLimit: function(namespace, ip, cb) {
mntmn's avatar
mntmn committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    const key = "limit_"+ namespace + "_"+ ip;
    const redis = this.connection;

    redis.get(key, (err, count)=> {
      if (count) {
        if(count < 150) {
          redis.incr(key, (err, newCount) => {
            if (newCount==150) {
              // limit
            }
            cb(true);
          });
        } else {
          cb(false);
        }
      } else {
        redis.set(key, 1, (err, count) => {
          redis.expire(key, 1800, (err, expResult) => {
            cb(true);
          });
        });
      }
    });
  }, 
mntmn's avatar
mntmn committed
139
  isOnlineInSpace: function(user, space, cb) {
mntmn's avatar
mntmn committed
140
141
142
143
144
145
146
147
148
149
150
    this.connection.smembers("space_" + space._id.toString(), function(err, list) {
      if (err) cb(err);
      else {
        var users = list.filter(function(item) {
          return user._id.toString() === item;
        });
        cb(null, (users.length > 0));
      }
    });
  }
};
mntmn's avatar
mntmn committed
151
152
153

return module.exports;