消息队列的回调,可以帮助我们知道任务的结果,无论是成功还是失败,我们可以根据结果执行不同的操作,
取消订阅的sdk源码
/**
* Unsubscribe for messages, stop receiving messages sent to destinations described by the filter.
*
* @name Paho.MQTT.Client#unsubscribe
* @function
* @param {string} filter - describing the destinations to receive messages from.
* @param {object} unsubscribeOptions - used to control the subscription
* @param {object} unsubscribeOptions.invocationContext - passed to the onSuccess callback
or onFailure callback.
* @param {function} unsubscribeOptions.onSuccess - called when the unsubscribe acknowledgement has been received from the server.
* A single response object parameter is passed to the
* onSuccess callback containing the following fields:
* <ol>
* <li>invocationContext - if set in the unsubscribeOptions.
* </ol>
* @param {function} unsubscribeOptions.onFailure called when the unsubscribe request has failed or timed out.
* A single response object parameter is passed to the onFailure callback containing the following fields:
* <ol>
* <li>invocationContext - if set in the unsubscribeOptions.
* <li>errorCode - a number indicating the nature of the error.
* <li>errorMessage - text describing the error.
* </ol>
* @param {number} unsubscribeOptions.timeout - which, if present, determines the number of seconds
* after which the onFailure callback is called. The presence of
* a timeout does not prevent the onSuccess callback from being
* called when the unsubscribe completes
* @throws {InvalidState} if the client is not in connected state.
*/
this.unsubscribe = function (filter, unsubscribeOptions) {
if (typeof filter !== "string")
throw new Error("Invalid argument:"+filter);
unsubscribeOptions = unsubscribeOptions || {} ;
validate(unsubscribeOptions, {invocationContext:"object",
onSuccess:"function",
onFailure:"function",
timeout:"number"
});
if (unsubscribeOptions.timeout && !unsubscribeOptions.onFailure)
throw new Error("unsubscribeOptions.timeout specified with no onFailure callback.");
client.unsubscribe(filter, unsubscribeOptions);
};
实际操作
let opt={
onSuccess(){
console.log("退订成功")
},
onFailure(error){
console.log("退订失败")
console.log(error)
}
}
this.client.unsubscribe(topic,opt);