refactored client ping-pong

This commit is contained in:
cubedro 2015-06-02 04:33:17 +03:00
parent da45dfbdf9
commit 69a619804d
4 changed files with 16 additions and 10 deletions

17
app.js
View File

@ -253,9 +253,9 @@ api.on('connection', function(spark) {
});
});
client.on('connection', function(clientSpark)
client.on('connection', function (clientSpark)
{
clientSpark.on('ready', function(data)
clientSpark.on('ready', function (data)
{
clientSpark.emit('init', { nodes: Nodes.all() });
@ -265,9 +265,9 @@ client.on('connection', function(clientSpark)
});
});
clientSpark.on('client-pong', function(data)
clientSpark.on('client-pong', function (data)
{
var latency = Math.ceil( (_.now() - clientLatency) / 2 );
var latency = Math.ceil( (_.now() - data.serverTime) / 2 );
clientSpark.emit('client-latency', { latency: latency });
});
@ -275,9 +275,12 @@ client.on('connection', function(clientSpark)
var latencyTimeout = setInterval( function ()
{
clientLatency = _.now();
client.write({ action: 'client-ping' });
client.write({
action: 'client-ping',
data: {
serverTime: _.now()
}
});
}, 5000);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -333,7 +333,10 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, socket, _, toastr)
break;
case "client-ping":
socket.emit('client-pong');
socket.emit('client-pong', {
serverTime: data.serverTime,
clientTime: _.now()
});
break;
}