Created
May 3, 2018 19:36
-
-
Save donilboudo/4c8b673beae0a66cd6a30c0a0e16db34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.JsonObject; | |
import com.pubnub.api.PNConfiguration; | |
import com.pubnub.api.PubNub; | |
import com.pubnub.api.callbacks.PNCallback; | |
import com.pubnub.api.callbacks.SubscribeCallback; | |
import com.pubnub.api.enums.PNStatusCategory; | |
import com.pubnub.api.models.consumer.PNPublishResult; | |
import com.pubnub.api.models.consumer.PNStatus; | |
import com.pubnub.api.models.consumer.pubsub.PNMessageResult; | |
import com.pubnub.api.models.consumer.pubsub.PNPresenceEventResult; | |
import java.util.Arrays; | |
import java.util.Collections; | |
public class TestPubNub { | |
public static void main(String[] args) { | |
PNConfiguration pnConfiguration = new PNConfiguration(); | |
pnConfiguration.setSubscribeKey("sub-c-ed795abe-47d5-11e8-863c-7a9da79a12b1"); | |
pnConfiguration.setPublishKey("pub-c-b476b0b1-2478-4481-bf47-de937fc6ee7b"); | |
pnConfiguration.setSecure(false); | |
PubNub pubnub = new PubNub(pnConfiguration); | |
// pubnub.subscribe().channels(Collections.singletonList("my_channel")).execute(); | |
pubnub.addListener(new SubscribeCallback() { | |
@Override | |
public void status(PubNub pubnub, PNStatus status) { | |
if (status.getCategory() == PNStatusCategory.PNUnexpectedDisconnectCategory) { | |
// This event happens when radio / connectivity is lost | |
} else if (status.getCategory() == PNStatusCategory.PNConnectedCategory) { | |
// Connect event. You can do stuff like publish, and know you'll get it. | |
// Or just use the connected event to confirm you are subscribed for | |
// UI / internal notifications, etc | |
if (status.getCategory() == PNStatusCategory.PNConnectedCategory) { | |
pubnub.publish() | |
.channel("awesomeChannel") | |
.message("hello!!") | |
.async(new PNCallback<PNPublishResult>() { | |
@Override | |
public void onResponse(PNPublishResult result, PNStatus status) { | |
// Check whether request successfully completed or not. | |
if (!status.isError()) { | |
System.out.println("pub timetoken: " + result.getTimetoken()); | |
} | |
System.out.println("pub status code: " + status.getStatusCode()); | |
} | |
}); | |
} | |
} else if (status.getCategory() == PNStatusCategory.PNReconnectedCategory) { | |
// Happens as part of our regular operation. This event happens when | |
// radio / connectivity is lost, then regained. | |
} else if (status.getCategory() == PNStatusCategory.PNDecryptionErrorCategory) { | |
// Handle messsage decryption error. Probably client configured to | |
// encrypt messages and on live data feed it received plain text. | |
} else if (status.getCategory() == PNStatusCategory.PNTimeoutCategory) { | |
System.out.println("Timeout, try to reconnect"); | |
// do some magic and call reconnect when ready | |
pubnub.reconnect(); | |
} else { | |
System.out.println(status.getCategory()); | |
} | |
} | |
@Override | |
public void message(PubNub pubnub, PNMessageResult message) { | |
// Handle new message stored in message.message | |
if (message.getChannel() != null) { | |
System.out.println("Channel message: " + message.getChannel()); | |
} else { | |
System.out.println("Subscription message: " + message.getSubscription()); | |
} | |
/* | |
log the following items with your favorite logger | |
- message.getMessage() | |
- message.getSubscription() | |
- message.getTimetoken() | |
*/ | |
} | |
@Override | |
public void presence(PubNub pubnub, PNPresenceEventResult presence) { | |
} | |
}); | |
pubnub.subscribe().channels(Collections.singletonList("awesomeChannel")).execute(); | |
} | |
private void publish(PubNub pubnub) { | |
JsonObject position = new JsonObject(); | |
position.addProperty("lat", 32L); | |
position.addProperty("lng", 32L); | |
System.out.println("before pub: " + position); | |
pubnub.publish() | |
.message(position) | |
.channel("my_channel") | |
.async(new PNCallback<PNPublishResult>() { | |
@Override | |
public void onResponse(PNPublishResult result, PNStatus status) { | |
// handle publish result, status always present, result if successful | |
// status.isError() to see if error happened | |
if (!status.isError()) { | |
System.out.println("pub timetoken: " + result.getTimetoken()); | |
} | |
System.out.println("pub status code: " + status.getStatusCode()); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment