cordova-plugin-mqtt-pahojs
Advanced tools
Comparing version
{ | ||
"name": "cordova-plugin-mqtt-pahojs", | ||
"cordova_name": "MQTT JavaScript Client", | ||
"version": "1.0.1", | ||
"version": "1.2.1", | ||
"description": "Pure JavaScript MQTT client library. Wrapper for paho.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -5,2 +5,3 @@ var Paho = require('./paho_mqttws31'); | ||
function MQTTClient(options) { | ||
EventEmitter(this); | ||
this.uri = options.uri || 'mqtt://' + options.host + ':' + (options.port || 8080); | ||
@@ -23,5 +24,8 @@ if (this.uri.substring(this.uri.length - 1) !== '/') this.uri += '/'; | ||
MQTTClient.prototype = new EventEmitter(); | ||
MQTTClient.prototype._showConnectionStatus = function(status) { | ||
if (status && this.reportConnectionStatus) | ||
window.plugins.toast.showLongBottom(status) | ||
window.plugins.toast.showLongBottom(status); | ||
this.emit('status', status); | ||
}; | ||
@@ -38,4 +42,3 @@ | ||
this.reconnectTry = 0; | ||
//console.log('-------_connectionLost restoreConnection[' + this.restoreConnection + ']disconnectNormally[' + this.disconnectNormally + ']'); | ||
window.plugins.mqtt_event_emitter.emit('disconnected'); | ||
this.emit('disconnected'); | ||
if (this.restoreConnection && !this.disconnectNormally) this.reconnect(); | ||
@@ -45,8 +48,12 @@ }; | ||
MQTTClient.prototype._onMessageArrived = function(msg) { | ||
window.plugins.mqtt_event_emitter.emit('data', msg); | ||
this.emit('data', msg); | ||
}; | ||
MQTTClient.prototype._shouldReconnect = function() { | ||
if (this.reconnectDelayIdx !== null && this.reconnectDelay[this.reconnectDelayIdx] === -1) | ||
return false; | ||
return true; | ||
} | ||
MQTTClient.prototype._startReconnectCounter = function(callback) { | ||
this._stopReconnectCounter(); | ||
this._incrementReconnectDelayIdx(); | ||
this.reconnectCounterCounted = 0; | ||
@@ -60,2 +67,3 @@ this._showReconnectToast(this.reconnectDelay[this.reconnectDelayIdx]); | ||
else { | ||
this.reconnectCounterCounted = 0; | ||
this._stopReconnectCounter(); | ||
@@ -85,36 +93,40 @@ callback && callback(); | ||
MQTTClient.prototype.connect = function() { | ||
MQTTClient.prototype.connect = function(reconnect) { | ||
if (!reconnect) | ||
this._markDisconnected(); | ||
this._stopReconnectCounter(); | ||
this._showConnectionStatus('Connecting...'); | ||
window.plugins.mqtt_event_emitter.emit('connecting'); | ||
this.emit('connecting'); | ||
this.disconnectNormally = false; | ||
const that = this; | ||
const promise = new Promise(function(resolve, reject) { | ||
//console.log('-----connect Promise'); | ||
return new Promise(function(resolve, reject) { | ||
function connectionFailed(error) { | ||
that.connected = false; | ||
if (that.restoreConnection && !that.disconnectNormally) { | ||
that._startReconnectCounter(that.reconnect.bind(that)); | ||
window.plugins.mqtt_event_emitter.emit('schedule_reconnect', that.reconnectDelay); | ||
this.connected = false; | ||
this._incrementReconnectDelayIdx(); | ||
if (this.restoreConnection && !this.disconnectNormally && this._shouldReconnect()) { | ||
this._startReconnectCounter(this.reconnect.bind(this)); | ||
this.emit('schedule_reconnect', this.reconnectDelay); | ||
} else { | ||
that._showConnectionStatus('Disconnected'); | ||
window.plugins.mqtt_event_emitter.emit('disconnected', error); | ||
this.disconnect(); | ||
} | ||
reject && reject(); | ||
} | ||
}; | ||
function connectionSuccess() { | ||
that._showConnectionStatus('Connected'); | ||
window.plugins.mqtt_event_emitter.emit('connected'); | ||
that.reconnectTry = 0; | ||
that.connected = true; | ||
this._showConnectionStatus('Connected'); | ||
this.emit('connected'); | ||
this.reconnectTry = 0; | ||
this.reconnectDelayIdx = null; | ||
this.connected = true; | ||
resolve && resolve(); | ||
}; | ||
var connectionParams = {onSuccess: connectionSuccess.bind(this), onFailure: connectionFailed.bind(this)}; | ||
if (this.keepAliveInterval) connectionParams.keepAliveInterval = this.keepAliveInterval; | ||
if (this.timeout) connectionParams.timeout = this.timeout; | ||
try { | ||
this.connection.connect(connectionParams); | ||
} catch (e) { | ||
reject && reject(e); | ||
} | ||
var connectionParams = {onSuccess: connectionSuccess, onFailure: connectionFailed}; | ||
if (that.keepAliveInterval) connectionParams.keepAliveInterval = that.keepAliveInterval; | ||
if (that.timeout) connectionParams.timeout = that.timeout; | ||
console.log('connectionParams.timeout == ' + connectionParams.timeout); | ||
that.connection.connect(connectionParams); | ||
}); | ||
return promise; | ||
}.bind(this)); | ||
}; | ||
@@ -130,12 +142,20 @@ | ||
this._setupConnection(); | ||
this.connect(); | ||
this.connect(true); | ||
} | ||
}; | ||
MQTTClient.prototype.disconnect = function() { | ||
window.plugins.mqtt_event_emitter.emit('disconnect'); | ||
MQTTClient.prototype._markDisconnected = function() { | ||
this.reconnectDelayIdx = null; | ||
this.disconnectNormally = true; | ||
this.reconnectTry = 0; | ||
this.connected = false; | ||
this.connection.disconnect(); | ||
} | ||
MQTTClient.prototype.disconnect = function() { | ||
this._stopReconnectCounter(); | ||
this.emit('disconnect'); | ||
this._markDisconnected(); | ||
if (this.connection.isConnected()) | ||
this.connection.disconnect(); | ||
this._showConnectionStatus('Disconnected'); | ||
}; | ||
@@ -163,10 +183,2 @@ | ||
cordova.addConstructor(function() { | ||
if (!window.plugins) { | ||
window.plugins = {}; | ||
} | ||
window.plugins.mqtt_event_emitter = new EventEmitter(); | ||
}); | ||
module.exports = MQTTClient; |
Sorry, the diff of this file is not supported yet
95060
0.06%2040
0.54%