app.js refactoring

This commit is contained in:
cubedro 2015-06-10 05:32:25 +03:00
parent ffcb50e196
commit a50a3b29f9

43
app.js
View File

@ -3,30 +3,27 @@ var logger = require('./lib/utils/logger');
var chalk = require('chalk'); var chalk = require('chalk');
var askedForHistoryTime = 0; var askedForHistoryTime = 0;
var Primus = require('primus'),
api,
client;
var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret"; var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret";
var Collection = require('./lib/collection');
var Nodes = new Collection();
var server;
var env = 'production';
// Init http server
if( process.env.NODE_ENV !== 'production' ) if( process.env.NODE_ENV !== 'production' )
{ {
var app = require('./lib/express'); var app = require('./lib/express');
server = require('http').createServer(app); server = require('http').createServer(app);
} }
else else
{
server = require('http').createServer(); server = require('http').createServer();
}
// Init socket vars
var Primus = require('primus');
var api;
var client;
var server;
// Init API Socket connection
api = new Primus(server, { api = new Primus(server, {
transformer: 'websockets', transformer: 'websockets',
pathname: '/api', pathname: '/api',
@ -36,13 +33,20 @@ api = new Primus(server, {
api.use('emit', require('primus-emit')); api.use('emit', require('primus-emit'));
api.use('spark-latency', require('primus-spark-latency')); api.use('spark-latency', require('primus-spark-latency'));
var client = new Primus(server, {
// Init Client Socket connection
client = new Primus(server, {
transformer: 'websockets', transformer: 'websockets',
pathname: '/primus', pathname: '/primus',
parser: 'JSON' parser: 'JSON'
}); });
var clientLatency = 0; client.use('emit', require('primus-emit'));
// Init collections
var Collection = require('./lib/collection');
var Nodes = new Collection();
Nodes.setChartsCallback(function (err, charts) Nodes.setChartsCallback(function (err, charts)
{ {
@ -59,8 +63,8 @@ Nodes.setChartsCallback(function (err, charts)
} }
}); });
client.use('emit', require('primus-emit'));
// Init API Socket events
api.on('connection', function (spark) api.on('connection', function (spark)
{ {
console.info('API', 'CON', 'Open:', spark.address.ip); console.info('API', 'CON', 'Open:', spark.address.ip);
@ -343,8 +347,7 @@ client.on('connection', function (clientSpark)
clientSpark.on('client-pong', function (data) clientSpark.on('client-pong', function (data)
{ {
var start = (!_.isUndefined(data) && !_.isUndefined(data.serverTime) ? data.serverTime : clientLatency); var latency = Math.ceil( (_.now() - data.serverTime) / 2 );
var latency = Math.ceil( (_.now() - start) / 2 );
clientSpark.emit('client-latency', { latency: latency }); clientSpark.emit('client-latency', { latency: latency });
}); });
@ -352,12 +355,10 @@ client.on('connection', function (clientSpark)
var latencyTimeout = setInterval( function () var latencyTimeout = setInterval( function ()
{ {
clientLatency = _.now();
client.write({ client.write({
action: 'client-ping', action: 'client-ping',
data: { data: {
serverTime: clientLatency serverTime: _.now()
} }
}); });
}, 5000); }, 5000);