Skip to content

Commit 3983e22

Browse files
author
Devendra
committed
heartbeat fix
1 parent ae9278b commit 3983e22

File tree

23 files changed

+876
-656
lines changed

23 files changed

+876
-656
lines changed

core/pubnub-common.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,14 @@ function PN_API(setup) {
301301
, SUB_ERROR = function(){}
302302
, STATE = {}
303303
, PRESENCE_HB_TIMEOUT = null
304+
/*
304305
, PRESENCE_HB = validate_presence_heartbeat(
305306
setup['heartbeat'] || setup['pnexpires'] || 0, setup['error']
306307
)
307308
, PRESENCE_HB_INTERVAL = setup['heartbeat_interval'] || PRESENCE_HB - 3
309+
*/
310+
, PRESENCE_HB = 0
311+
, PRESENCE_HB_INTERVAL = 0
308312
, PRESENCE_HB_RUNNING = false
309313
, NO_WAIT_FOR_PENDING = setup['no_wait_for_pending']
310314
, COMPATIBLE_35 = setup['compatible_3.5'] || false
@@ -359,6 +363,10 @@ function PN_API(setup) {
359363
function validate_presence_heartbeat(heartbeat, cur_heartbeat, error) {
360364
var err = false;
361365

366+
if (typeof heartbeat === 'undefined') {
367+
return cur_heartbeat;
368+
}
369+
362370
if (typeof heartbeat === 'number') {
363371
if (heartbeat > PRESENCE_HB_THRESHOLD || heartbeat == 0)
364372
err = false;
@@ -641,9 +649,12 @@ function PN_API(setup) {
641649
'get_heartbeat' : function() {
642650
return PRESENCE_HB;
643651
},
644-
'set_heartbeat' : function(heartbeat) {
645-
PRESENCE_HB = validate_presence_heartbeat(heartbeat, PRESENCE_HB_INTERVAL, error);
646-
PRESENCE_HB_INTERVAL = (PRESENCE_HB - 3 >= 1)?PRESENCE_HB - 3:1;
652+
'set_heartbeat' : function(heartbeat, heartbeat_interval) {
653+
PRESENCE_HB = validate_presence_heartbeat(heartbeat, PRESENCE_HB, error);
654+
PRESENCE_HB_INTERVAL = heartbeat_interval || (PRESENCE_HB / 2) - 1;
655+
if (PRESENCE_HB == 2) {
656+
PRESENCE_HB_INTERVAL = 1;
657+
}
647658
CONNECT();
648659
_presence_heartbeat();
649660
},
@@ -1096,6 +1107,7 @@ function PN_API(setup) {
10961107
, windowing = args['windowing'] || SUB_WINDOWING
10971108
, state = args['state']
10981109
, heartbeat = args['heartbeat'] || args['pnexpires']
1110+
, heartbeat_interval = args['heartbeat_interval']
10991111
, restore = args['restore'] || SUB_RESTORE;
11001112

11011113
// Restore Enabled?
@@ -1112,8 +1124,8 @@ function PN_API(setup) {
11121124
if (!callback) return error('Missing Callback');
11131125
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
11141126

1115-
if (heartbeat || heartbeat === 0) {
1116-
SELF['set_heartbeat'](heartbeat);
1127+
if (heartbeat || heartbeat === 0 || heartbeat_interval || heartbeat_interval === 0) {
1128+
SELF['set_heartbeat'](heartbeat, heartbeat_interval);
11171129
}
11181130

11191131
// Setup Channel(s)

modern/pubnub.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,14 @@ function PN_API(setup) {
302302
, SUB_ERROR = function(){}
303303
, STATE = {}
304304
, PRESENCE_HB_TIMEOUT = null
305+
/*
305306
, PRESENCE_HB = validate_presence_heartbeat(
306307
setup['heartbeat'] || setup['pnexpires'] || 0, setup['error']
307308
)
308309
, PRESENCE_HB_INTERVAL = setup['heartbeat_interval'] || PRESENCE_HB - 3
310+
*/
311+
, PRESENCE_HB = 0
312+
, PRESENCE_HB_INTERVAL = 0
309313
, PRESENCE_HB_RUNNING = false
310314
, NO_WAIT_FOR_PENDING = setup['no_wait_for_pending']
311315
, COMPATIBLE_35 = setup['compatible_3.5'] || false
@@ -360,6 +364,10 @@ function PN_API(setup) {
360364
function validate_presence_heartbeat(heartbeat, cur_heartbeat, error) {
361365
var err = false;
362366

367+
if (typeof heartbeat === 'undefined') {
368+
return cur_heartbeat;
369+
}
370+
363371
if (typeof heartbeat === 'number') {
364372
if (heartbeat > PRESENCE_HB_THRESHOLD || heartbeat == 0)
365373
err = false;
@@ -642,9 +650,12 @@ function PN_API(setup) {
642650
'get_heartbeat' : function() {
643651
return PRESENCE_HB;
644652
},
645-
'set_heartbeat' : function(heartbeat) {
646-
PRESENCE_HB = validate_presence_heartbeat(heartbeat, PRESENCE_HB_INTERVAL, error);
647-
PRESENCE_HB_INTERVAL = (PRESENCE_HB - 3 >= 1)?PRESENCE_HB - 3:1;
653+
'set_heartbeat' : function(heartbeat, heartbeat_interval) {
654+
PRESENCE_HB = validate_presence_heartbeat(heartbeat, PRESENCE_HB, error);
655+
PRESENCE_HB_INTERVAL = heartbeat_interval || (PRESENCE_HB / 2) - 1;
656+
if (PRESENCE_HB == 2) {
657+
PRESENCE_HB_INTERVAL = 1;
658+
}
648659
CONNECT();
649660
_presence_heartbeat();
650661
},
@@ -1097,6 +1108,7 @@ function PN_API(setup) {
10971108
, windowing = args['windowing'] || SUB_WINDOWING
10981109
, state = args['state']
10991110
, heartbeat = args['heartbeat'] || args['pnexpires']
1111+
, heartbeat_interval = args['heartbeat_interval']
11001112
, restore = args['restore'] || SUB_RESTORE;
11011113

11021114
// Restore Enabled?
@@ -1113,8 +1125,8 @@ function PN_API(setup) {
11131125
if (!callback) return error('Missing Callback');
11141126
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
11151127

1116-
if (heartbeat || heartbeat === 0) {
1117-
SELF['set_heartbeat'](heartbeat);
1128+
if (heartbeat || heartbeat === 0 || heartbeat_interval || heartbeat_interval === 0) {
1129+
SELF['set_heartbeat'](heartbeat, heartbeat_interval);
11181130
}
11191131

11201132
// Setup Channel(s)

0 commit comments

Comments
 (0)