Skip to content

Commit

Permalink
修改mqtt解绑topic
Browse files Browse the repository at this point in the history
  • Loading branch information
puyang1017 committed Mar 6, 2020
1 parent 52f0816 commit 8b019d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public interface IMqttStatusListener {
*/
void subscribeFail(IMqttToken iMqttToken, Throwable throwable);

/**
* 解绑订阅成功
*
* @param iMqttToken
*/
void unSubscribeSuccess(IMqttToken iMqttToken);

/**
* 解绑订阅失败
*
* @param iMqttToken
* @param throwable
*/
void unSubscribeFail(IMqttToken iMqttToken, Throwable throwable);

/**
* 链接完成
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void subscribe(List<String> topics, int subscribeQos) {

public void unSubscribe(String topic) {
try {
mqttClient.unsubscribe(topic, this.userContext, iMqttActionListener);
mqttClient.unsubscribe(topic, this.userContext, iMqttUnActionListener);
} catch (MqttException e) {
e.printStackTrace();
}
Expand All @@ -247,7 +247,7 @@ public void unSubscribe(List<String> topics) {
try {
int size = topics.size();
String[] subscribeTopics = topics.toArray(new String[size]);
mqttClient.unsubscribe(subscribeTopics, this.userContext, iMqttActionListener);
mqttClient.unsubscribe(subscribeTopics, this.userContext, iMqttUnActionListener);
} catch (MqttException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -288,6 +288,27 @@ public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
}
};

/**
* 订阅的动作后的回调
*/
private IMqttActionListener iMqttUnActionListener = new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
//System.out.println("订阅成功");
if (mIMqttStatusListener != null) {
mIMqttStatusListener.unSubscribeSuccess(iMqttToken);
}
}

@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
//System.out.println("订阅失败");
if (mIMqttStatusListener != null) {
mIMqttStatusListener.unSubscribeFail(iMqttToken, throwable);
}
}
};

/**
* 发布消息(默认topic)
*
Expand Down

0 comments on commit 8b019d5

Please sign in to comment.