fix multiple handlers
This commit is contained in:
parent
b825f51ce4
commit
2174f3e4be
@ -54,7 +54,7 @@ module.exports.broadcastSyncInfo = function(historicSync) {
|
||||
};
|
||||
|
||||
module.exports.broadcastMessage = function(from, to, ts, message) {
|
||||
console.log('sending socket: %s, %s, %s, %s', from, to, ts, message);
|
||||
console.log('sending socket: %s, %s, %s, %s', from, to, ts, JSON.stringify(message));
|
||||
if (ios) {
|
||||
ios.sockets.in(to).emit(from + '-' + ts, message);
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ var logger = require('./logger').logger;
|
||||
var util = require('util');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var sockets = require('../app/controllers/socket.js');
|
||||
var microtime = require('microtime');
|
||||
|
||||
var MESSAGE_PREFIX = 'msg-'; // msg-<sin1>-<sin2> => <message>
|
||||
|
||||
@ -25,14 +26,15 @@ var MessageDb = function(opts) {
|
||||
this.path = config.leveldb + '/messages' + (opts.name ? ('-' + opts.name) : '');
|
||||
this.db = opts.db || db || levelup(this.path, {
|
||||
maxOpenFiles: MAX_OPEN_FILES,
|
||||
valueEncoding : 'json'
|
||||
valueEncoding: 'json'
|
||||
});
|
||||
db = this.db;
|
||||
this.initEvents();
|
||||
db = this.db;
|
||||
};
|
||||
util.inherits(MessageDb, EventEmitter);
|
||||
|
||||
MessageDb.prototype.initEvents = function() {
|
||||
if (db) return;
|
||||
this.db.on('put', function(key, value) {
|
||||
var spl = key.split('-');
|
||||
var from = spl[1];
|
||||
@ -52,7 +54,7 @@ MessageDb.prototype.close = function(cb) {
|
||||
|
||||
|
||||
var messageKey = function(from, to, ts) {
|
||||
if (!ts) ts = Math.round(new Date().getTime());
|
||||
if (!ts) ts = Math.round(microtime.now());
|
||||
return MESSAGE_PREFIX + from.toString() + '-' + to.toString() + '-' + ts;
|
||||
};
|
||||
|
||||
@ -65,10 +67,10 @@ MessageDb.prototype.addMessage = function(m, from, to, cb) {
|
||||
MessageDb.prototype.getMessages = function(from, to, lower_ts, upper_ts, cb) {
|
||||
var list = [];
|
||||
var opts = {
|
||||
start: messageKey(from, to, upper_ts.getTime()),
|
||||
end: messageKey(from, to, lower_ts.getTime()),
|
||||
//limit: limit, TODO
|
||||
reverse: 1,
|
||||
end: messageKey(from, to, lower_ts),
|
||||
start: messageKey(from, to, upper_ts),
|
||||
// limit: limit, TODO
|
||||
reverse: true,
|
||||
};
|
||||
|
||||
db.createReadStream(opts)
|
||||
@ -76,7 +78,7 @@ MessageDb.prototype.getMessages = function(from, to, lower_ts, upper_ts, cb) {
|
||||
var spl = data.key.split('-');
|
||||
var from = spl[1];
|
||||
var to = spl[2];
|
||||
var ts = spl[3];
|
||||
var ts = +spl[3];
|
||||
list.push({
|
||||
ts: ts,
|
||||
message: data.value,
|
||||
|
||||
@ -82,6 +82,7 @@
|
||||
"grunt-mocha-test": "~0.8.1",
|
||||
"grunt-nodemon": "~0.2.0",
|
||||
"memdown": "^0.10.2",
|
||||
"microtime": "^0.6.0",
|
||||
"should": "2.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ var bitcore = require('bitcore');
|
||||
var SIN = bitcore.SIN;
|
||||
var levelup = require('levelup');
|
||||
var memdown = require('memdown');
|
||||
var microtime = require('microtime');
|
||||
|
||||
describe('MessageDb', function() {
|
||||
var opts = {
|
||||
@ -16,7 +17,7 @@ describe('MessageDb', function() {
|
||||
db: levelup({
|
||||
db: memdown,
|
||||
sync: true,
|
||||
valueEncoding : 'json'
|
||||
valueEncoding: 'json'
|
||||
})
|
||||
};
|
||||
it('should be able to create instance', function() {
|
||||
@ -33,20 +34,50 @@ describe('MessageDb', function() {
|
||||
a: 1,
|
||||
b: 2
|
||||
};
|
||||
it('should be able to add and read messages', function(done) {
|
||||
var message2 = {};
|
||||
var message3 = ['a', 'b'];
|
||||
it('should be able to add and read a message', function(done) {
|
||||
var mdb = new MessageDb(opts);
|
||||
var lower_ts = microtime.now();
|
||||
mdb.addMessage(message, from, to, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
var lower_ts = new Date('01/01/2014');
|
||||
var upper_ts = new Date();
|
||||
var upper_ts = microtime.now();
|
||||
mdb.getMessages(from, to, lower_ts, upper_ts, function(err, messages) {
|
||||
expect(err).to.not.exist;
|
||||
messages.length.should.equal(1);
|
||||
messages[0].ts.should.be.below(upper_ts);
|
||||
messages[0].ts.should.be.above(lower_ts);
|
||||
var m = messages[0].message;
|
||||
m.a.should.equal(1);
|
||||
m.b.should.equal(2);
|
||||
done();
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should be able to add many messages and read them', function(done) {
|
||||
var mdb = new MessageDb(opts);
|
||||
var lower_ts = microtime.now();
|
||||
mdb.addMessage(message, from, to, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
mdb.addMessage(message2, from, to, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
var upper_ts = microtime.now();
|
||||
setTimeout(function() {
|
||||
mdb.addMessage(message3, from, to, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
mdb.getMessages(from, to, lower_ts, upper_ts, function(err, messages) {
|
||||
expect(err).to.not.exist;
|
||||
messages.length.should.equal(2);
|
||||
messages[0].ts.should.be.below(upper_ts);
|
||||
messages[0].ts.should.be.above(lower_ts);
|
||||
JSON.stringify(messages[0].message).should.equal('{"a":1,"b":2}');
|
||||
messages[1].ts.should.be.below(upper_ts);
|
||||
messages[1].ts.should.be.above(lower_ts);
|
||||
JSON.stringify(messages[1].message).should.equal('{}');
|
||||
done();
|
||||
});
|
||||
});
|
||||
}, 10);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user