fix plugin bug

This commit is contained in:
Manuel Araoz 2014-08-20 13:05:52 -04:00
parent 4628f20f9f
commit 4f613ab4d0

View File

@ -9,45 +9,6 @@ module.exports.init = function(ext_io, config) {
io = ext_io;
io.sockets.on('connection', function(socket) {
// when it requests sync, send him all pending messages
socket.on('sync', function(ts) {
logger.debug('Sync requested by ' + socket.id);
logger.debug(' from timestamp ' + ts);
var rooms = socket.rooms;
if (rooms.length !== 2) {
socket.emit('insight-error', 'Must subscribe with public key before syncing');
return;
}
var to = rooms[1];
var upper_ts = Math.round(microtime.now());
logger.debug(' to timestamp ' + upper_ts);
mdb.getMessages(to, ts, upper_ts, function(err, messages) {
if (err) {
throw new Error('Couldn\'t get messages on sync request: ' + err);
}
logger.debug('\tFound ' + messages.length + ' message' + (messages.length !== 1 ? 's' : ''));
for (var i = 0; i < messages.length; i++) {
broadcastMessage(messages[i], socket);
}
});
});
// when it sends a message, add it to db
socket.on('message', function(m) {
logger.debug('Message sent from ' + m.pubkey + ' to ' + m.to);
mdb.addMessage(m, function(err) {
if (err) {
throw new Error('Couldn\'t add message to database: ' + err);
}
});
});
// disconnect handler
socket.on('disconnect', function() {
logger.debug('disconnected ' + socket.id);
});
mdb.on('message', broadcastMessage);
// when it requests sync, send him all pending messages
socket.on('sync', function(ts) {
logger.debug('Sync requested by ' + socket.id);
@ -81,7 +42,6 @@ module.exports.init = function(ext_io, config) {
});
});
// disconnect handler
socket.on('disconnect', function() {
logger.debug('disconnected ' + socket.id);