Skip to content

Commit 71fa56a

Browse files
committed
added approx latency detection (delay) for the _last_ delivered message.
1 parent 0f4ea5c commit 71fa56a

File tree

29 files changed

+834
-438
lines changed

29 files changed

+834
-438
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
06-25-13 * NEW VERSION 3.5.32 *
2+
. added approx latency detection (delay) for the _last_ delivered message.
3+
14
06-25-13 * NEW VERSION 3.5.3.1 *
25
. added noleave option to prevent explicit leave requests on page unload.
36

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.5.3.1
1+
3.5.32

core/pubnub-common.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var NOW = 1
55
, DEF_WINDOWING = 10 // MILLISECONDS.
66
, DEF_TIMEOUT = 10000 // MILLISECONDS.
77
, DEF_SUB_TIMEOUT = 310 // SECONDS.
8-
, DEF_KEEPALIVE = 60 // SECONDS.
8+
, DEF_KEEPALIVE = 60 // SECONDS (FOR TIMESYNC).
99
, SECOND = 1000 // A THOUSAND MILLISECONDS.
1010
, URLBIT = '/'
1111
, PARAMSBIT = '&'
@@ -199,6 +199,7 @@ function PN_API(setup) {
199199
, SUB_ORIGIN = nextorigin(ORIGIN)
200200
, CONNECT = function(){}
201201
, PUB_QUEUE = []
202+
, TIME_DRIFT = 0
202203
, SUB_CALLBACK = 0
203204
, SUB_CHANNEL = 0
204205
, SUB_RECEIVER = 0
@@ -465,6 +466,7 @@ function PN_API(setup) {
465466
, reconnect = args['reconnect'] || function(){}
466467
, disconnect = args['disconnect'] || function(){}
467468
, errcb = args['error'] || function(){}
469+
, idlecb = args['idle'] || function(){}
468470
, presence = args['presence'] || 0
469471
, noheresync = args['noheresync'] || 0
470472
, backfill = args['backfill'] || 0
@@ -598,6 +600,9 @@ function PN_API(setup) {
598600
return timeout( CONNECT, windowing );
599601
}
600602

603+
// User Idle Callback
604+
idlecb(messages[1]);
605+
601606
// Restore Previous Connection Point if Needed
602607
TIMETOKEN = !TIMETOKEN &&
603608
SUB_RESTORE &&
@@ -640,9 +645,10 @@ function PN_API(setup) {
640645
};
641646
})();
642647

648+
var latency = detect_latency(+messages[1]);
643649
each( messages[0], function(msg) {
644650
var next = next_callback();
645-
next[0]( msg, messages, next[1] );
651+
next[0]( msg, messages, next[1], latency );
646652
} );
647653

648654
timeout( _connect, windowing );
@@ -712,9 +718,10 @@ function PN_API(setup) {
712718

713719
function _poll_online2() {
714720
SELF['time'](function(success){
721+
detect_time_detla( function(){}, success );
715722
success || _reset_offline(1);
716723
timeout( _poll_online2, KEEPALIVE );
717-
})
724+
});
718725
}
719726

720727
function _reset_offline(err) {
@@ -728,7 +735,29 @@ function PN_API(setup) {
728735
timeout( _poll_online, SECOND );
729736
timeout( _poll_online2, KEEPALIVE );
730737

731-
SELF['time'](function() {});
738+
// Detect Age of Message
739+
function detect_latency(tt) {
740+
var adjusted_time = rnow() - TIME_DRIFT;
741+
return adjusted_time - tt / 10000;
742+
}
743+
744+
detect_time_detla();
745+
function detect_time_detla( cb, time ) {
746+
var stime = rnow();
747+
748+
time && calculate(time) || SELF['time'](calculate);
749+
750+
function calculate(time) {
751+
if (!time) return;
752+
753+
var ptime = time / 10000
754+
, latency = (rnow() - stime) / 2;
755+
756+
TIME_DRIFT = rnow() - (ptime + latency);
757+
758+
cb && cb(TIME_DRIFT);
759+
}
760+
}
732761

733762
return SELF;
734763
}

modern/pubnub-crypto.min.js

Lines changed: 23 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modern/pubnub.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// Version: 3.5.3.1
1+
// Version: 3.5.32
22
var NOW = 1
33
, READY = false
44
, READY_BUFFER = []
55
, PRESENCE_SUFFIX = '-pnpres'
66
, DEF_WINDOWING = 10 // MILLISECONDS.
77
, DEF_TIMEOUT = 10000 // MILLISECONDS.
88
, DEF_SUB_TIMEOUT = 310 // SECONDS.
9-
, DEF_KEEPALIVE = 60 // SECONDS.
9+
, DEF_KEEPALIVE = 60 // SECONDS (FOR TIMESYNC).
1010
, SECOND = 1000 // A THOUSAND MILLISECONDS.
1111
, URLBIT = '/'
1212
, PARAMSBIT = '&'
@@ -200,6 +200,7 @@ function PN_API(setup) {
200200
, SUB_ORIGIN = nextorigin(ORIGIN)
201201
, CONNECT = function(){}
202202
, PUB_QUEUE = []
203+
, TIME_DRIFT = 0
203204
, SUB_CALLBACK = 0
204205
, SUB_CHANNEL = 0
205206
, SUB_RECEIVER = 0
@@ -466,6 +467,7 @@ function PN_API(setup) {
466467
, reconnect = args['reconnect'] || function(){}
467468
, disconnect = args['disconnect'] || function(){}
468469
, errcb = args['error'] || function(){}
470+
, idlecb = args['idle'] || function(){}
469471
, presence = args['presence'] || 0
470472
, noheresync = args['noheresync'] || 0
471473
, backfill = args['backfill'] || 0
@@ -599,6 +601,9 @@ function PN_API(setup) {
599601
return timeout( CONNECT, windowing );
600602
}
601603

604+
// User Idle Callback
605+
idlecb(messages[1]);
606+
602607
// Restore Previous Connection Point if Needed
603608
TIMETOKEN = !TIMETOKEN &&
604609
SUB_RESTORE &&
@@ -641,9 +646,10 @@ function PN_API(setup) {
641646
};
642647
})();
643648

649+
var latency = detect_latency(+messages[1]);
644650
each( messages[0], function(msg) {
645651
var next = next_callback();
646-
next[0]( msg, messages, next[1] );
652+
next[0]( msg, messages, next[1], latency );
647653
} );
648654

649655
timeout( _connect, windowing );
@@ -713,9 +719,10 @@ function PN_API(setup) {
713719

714720
function _poll_online2() {
715721
SELF['time'](function(success){
722+
detect_time_detla( function(){}, success );
716723
success || _reset_offline(1);
717724
timeout( _poll_online2, KEEPALIVE );
718-
})
725+
});
719726
}
720727

721728
function _reset_offline(err) {
@@ -729,7 +736,29 @@ function PN_API(setup) {
729736
timeout( _poll_online, SECOND );
730737
timeout( _poll_online2, KEEPALIVE );
731738

732-
SELF['time'](function() {});
739+
// Detect Age of Message
740+
function detect_latency(tt) {
741+
var adjusted_time = rnow() - TIME_DRIFT;
742+
return adjusted_time - tt / 10000;
743+
}
744+
745+
detect_time_detla();
746+
function detect_time_detla( cb, time ) {
747+
var stime = rnow();
748+
749+
time && calculate(time) || SELF['time'](calculate);
750+
751+
function calculate(time) {
752+
if (!time) return;
753+
754+
var ptime = time / 10000
755+
, latency = (rnow() - stime) / 2;
756+
757+
TIME_DRIFT = rnow() - (ptime + latency);
758+
759+
cb && cb(TIME_DRIFT);
760+
}
761+
}
733762

734763
return SELF;
735764
}
@@ -768,7 +797,7 @@ THE SOFTWARE.
768797
* UTIL LOCALS
769798
*/
770799
var NOW = 1
771-
, PNSDK = 'PubNub-JS-' + 'Modern' + '/' + '3.5.3.1'
800+
, PNSDK = 'PubNub-JS-' + 'Modern' + '/' + '3.5.32'
772801
, XHRTME = 310000;
773802

774803

0 commit comments

Comments
 (0)