MqttX.js 2.53 KB
var mqttX = {
	client:null,
	mqttStatus:false,
	options:{},
	onConnect:function(){
		mqttStatus = true;
		if(options && options.success){
			options.success();
		}
	},
	onFailure:function(){
		mqttStatus = false;
		if(options && options.error){
			options.error();
		}
	},
	onConnectionLost:function(responseObject){
		if (responseObject.errorCode !== 0) {
            console.log("onConnectionLost:"+responseObject.errorMessage);
            console.log("连接已断开");
            mqttStatus = false;
			if(options && options.connectLost){
				options.connectLost(responseObject.errorMessage);
			}
        }
	},
	onMessageArrived:function(message){
		//console.log(message);
		if(options && options.onMessage){
			options.onMessage(message);
		}
	},
	sendMessage:function(topic,obj){
		if(mqttStatus){
			var message = new Paho.MQTT.Message(JSON.stringify(obj));
			message.destinationName = topic;
			if(mqttX.client && mqttStatus){
				mqttX.client.send(message);				
				return true;				
			}
		}
		return false;
	},
	subscribe:function(topic,qos){
		if(mqttX.client && mqttStatus){
			mqttX.client.subscribe(topic,{qos:qos});
			return true;
		}
		return false;
	},
	unsubscribe:function(topic){
		if(mqttX.client && mqttStatus){
			mqttX.client.unsubscribe(topic);//取消订阅主题
			return true;			
		}
		return false;
	},
	disconnect:function(){
		if(mqttX.client && mqttStatus){
			mqttX.client.disconnect();//取消订阅主题
			return true;			
		}
		return false;
	},
	reconnect:function(){
		if(mqttX.client && options){
			var useSSL = false;
			if(options.useSSL){
				useSSL = options.useSSL;
			}
			
			mqttX.client.connect({
				onSuccess:this.onConnect,
		    	onFailure: this.onFailure,
				timeout: 10,
				keepAliveInterval:100,
				cleanSession:true,
				useSSL:useSSL
			});
			return true;			
		}
		return false;
	},
	isConnected:function(){
		if(mqttX.client){
			return mqttX.client.isConnected();	
		}
		return false;
	},
	init:function(options1){
		if(options1){
			options = options1;
			
			var useSSL = false;
			if(options.useSSL){
				useSSL = options.useSSL;
			}
			mqttX.client = new Paho.MQTT.Client(options.ip, Number(options.port), options.id);
			mqttX.client.connect({
		    	onSuccess:this.onConnect,
		    	onFailure: this.onFailure,
				timeout: 10,
				keepAliveInterval:100,
				cleanSession:true,
				useSSL:useSSL
		    });		
		    mqttX.client.onConnectionLost = this.onConnectionLost;//注册连接断开处理事件
			mqttX.client.onMessageArrived = this.onMessageArrived;//注册消息接收处理事件
		}
		
	}
};