From b9a9321caccb993bfd70e2ce1b67daca642dc989 Mon Sep 17 00:00:00 2001 From: cubedro Date: Tue, 2 Jun 2015 04:49:34 +0300 Subject: [PATCH 1/2] fixed client latency --- app.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 4d66dff..2744a83 100644 --- a/app.js +++ b/app.js @@ -267,6 +267,7 @@ client.on('connection', function (clientSpark) clientSpark.on('client-pong', function (data) { + var start = (!_.isUndefined(data) && !_.isUndefined(data.serverTime) ? data.serverTime : clientLatency) var latency = Math.ceil( (_.now() - data.serverTime) / 2 ); clientSpark.emit('client-latency', { latency: latency }); @@ -275,10 +276,12 @@ client.on('connection', function (clientSpark) var latencyTimeout = setInterval( function () { + clientLatency = _.now(); + client.write({ action: 'client-ping', data: { - serverTime: _.now() + serverTime: clientLatency } }); }, 5000); From 11cc9897fbdb5484edffb995775796c12503f5c4 Mon Sep 17 00:00:00 2001 From: cubedro Date: Tue, 2 Jun 2015 04:53:26 +0300 Subject: [PATCH 2/2] fixed api latency bug --- app.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 2744a83..04bc6b4 100644 --- a/app.js +++ b/app.js @@ -211,8 +211,10 @@ api.on('connection', function(spark) { spark.on('node-ping', function(data) { + var start = (!_.isUndefined(data) && !_.isUndefined(data.clientTime) ? data.clientTime : null); + spark.emit('node-pong', { - clientTime: data.clientTime, + clientTime: start, serverTime: _.now() }); }); @@ -267,7 +269,7 @@ client.on('connection', function (clientSpark) clientSpark.on('client-pong', function (data) { - var start = (!_.isUndefined(data) && !_.isUndefined(data.serverTime) ? data.serverTime : clientLatency) + var start = (!_.isUndefined(data) && !_.isUndefined(data.serverTime) ? data.serverTime : clientLatency); var latency = Math.ceil( (_.now() - data.serverTime) / 2 ); clientSpark.emit('client-latency', { latency: latency });