Java Pubnub V4迁移回调
我正在尝试将我的代码从pubnub sdk v3更新到v4,我被回调卡住了 我想更新以下功能:Java Pubnub V4迁移回调,java,android,pubnub,Java,Android,Pubnub,我正在尝试将我的代码从pubnub sdk v3更新到v4,我被回调卡住了 我想更新以下功能: void transmitMessage(String toID, JSONObject packet){ if (this.id==null){ . mRtcListener.onDebug(new PnRTCMessage("Cannot transmit before calling Client.connect")); } try {
void transmitMessage(String toID, JSONObject packet){
if (this.id==null){ .
mRtcListener.onDebug(new PnRTCMessage("Cannot transmit before calling Client.connect"));
}
try {
JSONObject message = new JSONObject();
message.put(PnRTCMessage.JSON_PACKET, packet);
message.put(PnRTCMessage.JSON_ID, "");
message.put(PnRTCMessage.JSON_NUMBER, this.id);
this.mPubNub.publish(toID, message, new Callback() {
@Override
public void successCallback(String channel, Object message, String timetoken) {
mRtcListener.onDebug(new PnRTCMessage((JSONObject)message));
}
@Override
public void errorCallback(String channel, PubNubError error) {
mRtcListener.onDebug(new PnRTCMessage(error.errorObject));
}
});
} catch (JSONException e){
e.printStackTrace();
}
}
文档中说不需要实例化com.pubnub.api.Callback,应该使用新的SubscribeCallback类。我不知道如何处理它,SubscribeCallback包含以下方法:Status、message和presence,目前我有一个successCallback方法和一个errorCallback。中的代码应该可以帮助您完成这项工作
您可以使用以下代码创建侦听器:
pubnub.addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
switch (status.getOperation()) {
// let's combine unsubscribe and subscribe handling for ease of use
case PNSubscribeOperation:
case PNUnsubscribeOperation:
// note: subscribe statuses never have traditional
// errors, they just have categories to represent the
// different issues or successes that occur as part of subscribe
switch (status.getCategory()) {
case PNConnectedCategory:
// this is expected for a subscribe, this means there is no error or issue whatsoever
case PNReconnectedCategory:
// this usually occurs if subscribe temporarily fails but reconnects. This means
// there was an error but there is no longer any issue
case PNDisconnectedCategory:
// this is the expected category for an unsubscribe. This means there
// was no error in unsubscribing from everything
case PNUnexpectedDisconnectCategory:
// this is usually an issue with the internet connection, this is an error, handle appropriately
case PNAccessDeniedCategory:
// this means that PAM does allow this client to subscribe to this
// channel and channel group configuration. This is another explicit error
default:
// More errors can be directly specified by creating explicit cases for other
// error categories of `PNStatusCategory` such as `PNTimeoutCategory` or `PNMalformedFilterExpressionCategory` or `PNDecryptionErrorCategory`
}
case PNHeartbeatOperation:
// heartbeat operations can in fact have errors, so it is important to check first for an error.
// For more information on how to configure heartbeat notifications through the status
// PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
if (status.isError()) {
// There was an error with the heartbeat operation, handle here
} else {
// heartbeat operation was successful
}
default: {
// Encountered unknown status type
}
}
}
@Override
public void message(PubNub pubnub, PNMessageResult message) {
String messagePublisher = message.getPublisher();
System.out.println("Message publisher: " + messagePublisher);
System.out.println("Message Payload: " + message.getMessage());
System.out.println("Message Subscription: " + message.getSubscription());
System.out.println("Message Channel: " + message.getChannel());
System.out.println("Message timetoken: " + message.getTimetoken());
}
@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
}
请注意,我们最近还推出了新功能和新类型的侦听器,所有这些功能都列在上面的链接中。@hunjm-段落提供的内容是正确的。我认为您可能会遇到的挑战是,我们将回调从您进行的每个订阅调用中移动到了一个集中的侦听器。这可能需要您重构回调处理代码,以便从每个订阅模型接收消息、连接成功/失败和存在事件,并将其发送到此中心侦听器模型。主要只是需要移动代码,如果您需要对每个频道进行不同的处理,可能需要检查频道名称。如果您有进一步的挑战,请随时与我们一起打开支持票证,我们可以提供进一步的指导。
pubnub.subscribe()
.channels(Arrays.asList("my_channel")) // subscribe to channels
.withPresence() // also subscribe to related presence information
.execute();