* connect - connect to an MQTT broker. * * @param {String} [brokerUrl] - url of the broker, optional * @param {Object} opts - see MqttClient#constructor
(brokerUrl, opts)
| 1114 | * @param {Object} opts - see MqttClient#constructor |
| 1115 | */ |
| 1116 | function connect (brokerUrl, opts) { |
| 1117 | |
| 1118 | if (('object' === typeof brokerUrl) && !opts) { |
| 1119 | opts = brokerUrl; |
| 1120 | brokerUrl = null; |
| 1121 | } |
| 1122 | |
| 1123 | opts = opts || {}; |
| 1124 | |
| 1125 | if (brokerUrl) { |
| 1126 | opts = xtend(url.parse(brokerUrl, true), opts); |
| 1127 | opts.protocol = opts.protocol.replace(/\:$/, ''); |
| 1128 | } |
| 1129 | |
| 1130 | // merge in the auth options if supplied |
| 1131 | parseAuthOptions(opts); |
| 1132 | |
| 1133 | // support clientId passed in the query string of the url |
| 1134 | if (opts.query && 'string' === typeof opts.query.clientId) { |
| 1135 | opts.clientId = opts.query.clientId; |
| 1136 | } |
| 1137 | |
| 1138 | if (opts.cert && opts.key) { |
| 1139 | if (opts.protocol) { |
| 1140 | if (-1 === ['mqtts', 'wss'].indexOf(opts.protocol)) { |
| 1141 | /* |
| 1142 | * jshint and eslint |
| 1143 | * complains that break from default cannot be reached after throw |
| 1144 | * it is a foced exit from a control structure |
| 1145 | * maybe add a check after switch to see if it went through default |
| 1146 | * and then throw the error |
| 1147 | */ |
| 1148 | /*jshint -W027*/ |
| 1149 | /*eslint no-unreachable:1*/ |
| 1150 | switch (opts.protocol) { |
| 1151 | case 'mqtt': |
| 1152 | opts.protocol = 'mqtts'; |
| 1153 | break; |
| 1154 | case 'ws': |
| 1155 | opts.protocol = 'wss'; |
| 1156 | break; |
| 1157 | default: |
| 1158 | throw new Error('Unknown protocol for secure conenction: "' + opts.protocol + '"!'); |
| 1159 | break; |
| 1160 | } |
| 1161 | /*eslint no-unreachable:0*/ |
| 1162 | /*jshint +W027*/ |
| 1163 | } |
| 1164 | } else { |
| 1165 | // don't know what protocol he want to use, mqtts or wss |
| 1166 | throw new Error('Missing secure protocol key'); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | if (!protocols[opts.protocol]) { |
| 1171 | opts.protocol = protocolList.filter(function (key) { |
| 1172 | return 'function' === typeof protocols[key]; |
| 1173 | })[0]; |
no test coverage detected