function connect (callback) { var simpl = require('simpl') var client = simpl.createClient() client.use(simpl.events()) client.use(simpl.json()) client.on('connect', callback) }
maptail.attach = function (app) { var users = { list: [] , has: function (socket) { return !!~this.list.indexOf(socket) } , add: function (socket) { if (!this.has(socket)) this.list.push(socket) } , remove: function (socket) { var index = this.list.indexOf(socket) if (index > -1) this.list.splice(index, 1) } , forEach: function (fn) { this.list.forEach(fn) } } var simpl = require('simpl') var ws = simpl.createServer(app) ws.use(simpl.events()) ws.use(simpl.json()) ws.on('connection', function (socket) { socket.on('close', function () { users.remove(socket) }) socket.remote .on('subscribe', function (what) { if (what === 'geoip') { users.add(socket) maptail.config.dateNow = Date.now() socket.remote.emit('config', maptail.config) socket.remote.emit('geoip', maptail.history) } }) .on('unsubscribe', function (what) { if (what === 'geoip') users.remove(socket) }) }) var before = Date.now() maptail.on('geoip', function (geo) { maptail.history.push(geo) maptail.buffer.push(geo) if (maptail.history.length > maptail.config.historyMax) maptail.history.shift() if (Date.now() - before > maptail.config.bufferTime && maptail.buffer.length) { users.forEach(function (socket) { socket.remote.emit('geoip', maptail.buffer) }) maptail.buffer = [] before = Date.now() } else { if (maptail.buffer.length > maptail.config.bufferMax) maptail.buffer.shift() } }) }
maptail.attach = function (app) { var users = { list: [] , has: function (socket) { return !!~this.list.indexOf(socket) } , add: function (socket) { if (!this.has(socket)) { this.list.push(socket) if (!maptail.config.quiet) console.log('>> New client: ' + socket.remoteAddress) } } , remove: function (socket) { var index = this.list.indexOf(socket) if (index > -1) { this.list.splice(index, 1) if (!maptail.config.quiet) console.log('>> client has quit: ' + socket.remoteAddress) } } , forEach: function (fn) { this.list.forEach(fn) } } var simpl = require('simpl') var ws = simpl.createServer(app) ws.use(simpl.events()) ws.use(simpl.json()) ws.on('connection', function (socket) { socket.on('close', function () { users.remove(socket) }) socket.remote .on('subscribe', function (what) { if (what === 'geoip') { users.add(socket) maptail.config.dateNow = Date.now() socket.remote.emit('config', maptail.config) socket.remote.emit('geoip', maptail.history) } }) .on('unsubscribe', function (what) { if (what === 'geoip') users.remove(socket) }) }) var before = Date.now() maptail.on('geoip', function (geo) { maptail.history.push(geo) maptail.buffer.push(geo) // do not emit duplicate IPs in a same packet if (maptail.config.noduplicates && geo.ip) { maptail.ipbuffer.contains(geo.ip, function(found) { if(found) { maptail.buffer.shift() maptail.duplicates += 1 } else { maptail.ipbuffer.push(geo.ip) } }) } if (geo.sound) { users.forEach(function (socket) { socket.remote.emit('geoip', [geo]) }) maptail.history.pop() maptail.buffer.pop() } else { if (maptail.history.length > maptail.config.historyMax) maptail.history.shift() //if (maptail.buffer.length && (maptail.buffer.length >= maptail.config.bufferMax || Date.now() - before > maptail.config.bufferTime && maptail.buffer.length)) if (maptail.buffer.length && (Date.now() - before > maptail.config.bufferTime && maptail.buffer.length)) { if (!maptail.config.quiet) console.log('Emit a packet of ' + maptail.buffer.length + ' objects, ' + (maptail.config.noduplicates ? maptail.duplicates + ' duplicate IP, ' : '') + maptail.discards + ' discarded, ' + users.list.length + ' connected users') users.forEach(function (socket) { socket.remote.emit('geoip', maptail.buffer) }) before = Date.now() maptail.buffer = [] maptail.ipbuffer = [] maptail.duplicates = 0 maptail.discards = 0 } else { if (geo.ip && maptail.buffer.length > maptail.config.bufferMax) { maptail.buffer.pop() maptail.discards += 1 } } } }) }