diff --git a/README.md b/README.md index 1ecfb1e..393c5bb 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,10 @@ settings = { json: false, mqtt: require('mqtt'), host: '127.0.0.1', - port: 1883 + port: 1883, + keyPath: 'path/to/cert.key', + certPath: 'path/to/cert.cert', + rejectUnauthorized: true }; ascoltatori.build(settings, function (ascoltatore) { diff --git a/lib/mqtt_ascoltatore.js b/lib/mqtt_ascoltatore.js index c008d12..07e746a 100644 --- a/lib/mqtt_ascoltatore.js +++ b/lib/mqtt_ascoltatore.js @@ -18,7 +18,9 @@ var SubsCounter = require("./subs_counter"); * - `port`, the port to connect to; * - `host`, the host to connect to; * - `mqtt`, the mqtt module (it will automatically be required if not present). - * + * - `keyPath`, path to TLS-key (optional, used for connecting to a secure broker). + * - `certPath`, path to TLS-certificate (optional, for connecting to a secure broker). + * - `rejectUnauthorized`, Reject unauthorized connections (optional, for connecting to a secure broker). * @api public * @param {Object} opts The options object */ @@ -50,15 +52,23 @@ MQTTAscoltatore.prototype = Object.create(AbstractAscoltatore.prototype); */ MQTTAscoltatore.prototype._startConn = function() { var that = this, - settings = null; + settings = null, + createMethod = 'createClient'; if (this._client === undefined) { settings = { keepalive: that._opts.keepalive, - clientId: that._opts.clientId, + clientId: that._opts.clientId }; + if(that._opts.keyPath) settings.keyPath = that._opts.keyPath; + if(that._opts.certPath) settings.certPath = that._opts.certPath; + if(that._opts.rejectUnauthorized) settings.rejectUnauthorized = that._opts.rejectUnauthorized; + if(settings.certPath || settings.keyPath) { + createMethod = 'createSecureClient'; + } + debug("connecting.."); - this._client = that._opts.mqtt.createClient(that._opts.port, that._opts.host, settings); + this._client = that._opts.mqtt[createMethod](that._opts.port, that._opts.host, settings); this._client.setMaxListeners(0); this._client.on("connect", function() { debug("connected"); diff --git a/package.json b/package.json index 286fc81..d7ceefc 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "sinon": "~1.7.3", "sinon-chai": "~2.4.0", "optimist": "~0.6.0", - "async_bench": "~0.1.0", + "async_bench": "~0.3.0", "dox-foundation": "0.5.4", "mosca": "git://github.com/mcollina/mosca.git", "jshint": "~2.3.0",