Sitelet https://github.com/NorwayCoding/java/commit/06dcd5838b277ca44343716c2435be0f2287af2b
Skip to content

Commit 06dcd58

Browse files
author
Devendra
committed
adding presence heartbeat interval
1 parent 087a5af commit 06dcd58

3 files changed

Lines changed: 62 additions & 11 deletions

File tree

‎android/examples/PubnubExample/src/com/pubnub/examples/pubnubExample/MainActivity.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,36 @@ public boolean onOptionsItemSelected(MenuItem item) {
182182
case R.id.option21:
183183
setUUID();
184184
return true;
185+
case R.id.option22:
186+
setState();
187+
return true;
188+
case R.id.option23:
189+
setHeartbeatInterval();
190+
return true;
185191
default:
186192
return super.onOptionsItemSelected(item);
187193
}
188194
}
189195

196+
private void setHeartbeatInterval() {
197+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
198+
builder.setTitle("Set Presence Heartbeat Interval");
199+
builder.setMessage("Enter heartbeat value in seconds");
200+
final EditText edTimeout = new EditText(this);
201+
edTimeout.setInputType(InputType.TYPE_CLASS_NUMBER);
202+
builder.setView(edTimeout);
203+
builder.setPositiveButton("Done",
204+
new DialogInterface.OnClickListener() {
205+
@Override
206+
public void onClick(DialogInterface dialog, int which) {
207+
pubnub.setHeartbeatInterval(Integer.parseInt(edTimeout.getText().toString()));
208+
}
209+
});
210+
AlertDialog alert = builder.create();
211+
alert.show();
212+
213+
}
214+
190215
private void setHeartbeat() {
191216
AlertDialog.Builder builder = new AlertDialog.Builder(this);
192217
builder.setTitle("Set Presence Heartbeat");

‎java/examples/src/com/pubnub/examples/PubnubDemoConsole.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,16 @@ public void errorCallback(String channel, PubnubError error) {
395395
});
396396
break;
397397
case 28:
398-
getState();
398+
int heartbeatInterval = getIntFromConsole("Pubnub Presence Heartbeat Interval ( in seconds ), Current value : " + pubnub.getHeartbeatInterval());
399+
pubnub.setHeartbeatInterval(heartbeatInterval);
399400
break;
400401
case 29:
401-
setState();
402+
getState();
402403
break;
403404
case 30:
405+
setState();
406+
break;
407+
case 31:
404408
String uid = getStringFromConsole("UUID", true);
405409
if (uid == null || uid.length() == 0) uid = pubnub.getUUID();
406410
whereNow(uid);
@@ -672,9 +676,10 @@ private void displayMenuOptions() {
672676
notifyUser("ENTER 25 FOR Disabling Cache Busting ( current: " + pubnub.getCacheBusting() + " )");
673677
notifyUser("ENTER 26 FOR Setting UUID ( current: " + pubnub.getUUID() + " )");
674678
notifyUser("ENTER 27 FOR Setting Presence Heartbeat ( current: " + pubnub.getHeartbeat() + " )");
675-
notifyUser("ENTER 28 FOR Getting Subscriber State");
676-
notifyUser("ENTER 29 FOR Setting Subscriber State");
677-
notifyUser("ENTER 30 FOR Where Now");
679+
notifyUser("ENTER 28 FOR Setting Presence Heartbeat Interval ( current: " + pubnub.getHeartbeatInterval() + " )");
680+
notifyUser("ENTER 29 FOR Getting Subscriber State");
681+
notifyUser("ENTER 30 FOR Setting Subscriber State");
682+
notifyUser("ENTER 31 FOR Where Now");
678683
notifyUser("\nENTER 0 to display this menu");
679684
}
680685

‎java/srcPubnubApi/com/pubnub/api/PubnubCore.java‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ abstract class PubnubCore {
3434
protected String SECRET_KEY = "";
3535
private String CIPHER_KEY = "";
3636
private String IV = null;
37-
private int PRESENCE_HB_INTERVAL = 0;
3837
private volatile String AUTH_STR = null;
3938
private volatile boolean CACHE_BUSTING = true;
4039
protected Hashtable params;
@@ -58,8 +57,9 @@ abstract class PubnubCore {
5857

5958
protected abstract String getUserAgent();
6059

61-
int PRESENCE_HEARTBEAT_TASK = 0;
62-
int HEARTBEAT = 320;
60+
private int PRESENCE_HEARTBEAT_TASK = 0;
61+
private int HEARTBEAT = 320;
62+
private volatile int PRESENCE_HB_INTERVAL = 0;
6363

6464
/**
6565
* This method when called stops Pubnub threads
@@ -202,13 +202,15 @@ public void setHeartbeat(int heartbeat, Callback callback) {
202202
Callback cb = getWrappedCallback(callback);
203203

204204
PRESENCE_HB_INTERVAL = (heartbeat > 0 && heartbeat < 5)?5:heartbeat;
205+
PRESENCE_HB_INTERVAL = (PRESENCE_HB_INTERVAL - 3 >= 1)?PRESENCE_HB_INTERVAL:1;
206+
205207
if (PRESENCE_HEARTBEAT_TASK == 0) {
206208
PRESENCE_HEARTBEAT_TASK = timedTaskManager.addTask("Presence-Heartbeat",
207-
new PresenceHeartbeatTask(heartbeat - 3, cb));
208-
} else if (heartbeat == 0 || heartbeat > 320) {
209+
new PresenceHeartbeatTask(PRESENCE_HB_INTERVAL, cb));
210+
} else if (PRESENCE_HB_INTERVAL == 0 || PRESENCE_HB_INTERVAL > 320) {
209211
timedTaskManager.removeTask(PRESENCE_HEARTBEAT_TASK);
210212
} else {
211-
timedTaskManager.updateTask(PRESENCE_HEARTBEAT_TASK, heartbeat - 3);
213+
timedTaskManager.updateTask(PRESENCE_HEARTBEAT_TASK, PRESENCE_HB_INTERVAL);
212214
}
213215
HEARTBEAT = heartbeat;
214216
disconnectAndResubscribe();
@@ -221,6 +223,25 @@ public void setPnExpires(int pnexpires) {
221223
public void setHeartbeat(int heartbeat) {
222224
setHeartbeat(heartbeat, null);
223225
}
226+
227+
public void setHeartbeatInterval(int heartbeatInterval) {
228+
229+
Callback cb = getWrappedCallback(null);
230+
PRESENCE_HB_INTERVAL = heartbeatInterval;
231+
if (PRESENCE_HEARTBEAT_TASK == 0) {
232+
PRESENCE_HEARTBEAT_TASK = timedTaskManager.addTask("Presence-Heartbeat",
233+
new PresenceHeartbeatTask(PRESENCE_HB_INTERVAL, cb));
234+
} else if (PRESENCE_HB_INTERVAL == 0 || PRESENCE_HB_INTERVAL > 320) {
235+
timedTaskManager.removeTask(PRESENCE_HEARTBEAT_TASK);
236+
} else {
237+
timedTaskManager.updateTask(PRESENCE_HEARTBEAT_TASK, PRESENCE_HB_INTERVAL);
238+
}
239+
240+
}
241+
242+
public int getHeartbeatInterval() {
243+
return PRESENCE_HB_INTERVAL;
244+
}
224245

225246

226247
/**

0 commit comments

Comments
 (0)