[apps/tidep0084.git] / example / iot-gateway / node_modules / aws-iot-device-sdk / node_modules / mqtt / lib / connect / index.js
1 'use strict';
2 var MqttClient = require('../client'),
3 url = require('url'),
4 xtend = require('xtend'),
5 protocols = {},
6 protocolList = [];
8 if ('browser' !== process.title) {
9 protocols.mqtt = require('./tcp');
10 protocols.tcp = require('./tcp');
11 protocols.ssl = require('./tls');
12 protocols.tls = require('./tls');
13 protocols.mqtts = require('./tls');
14 }
16 protocols.ws = require('./ws');
17 protocols.wss = require('./ws');
19 protocolList = [
20 'mqtt',
21 'mqtts',
22 'ws',
23 'wss'
24 ];
27 /**
28 * Parse the auth attribute and merge username and password in the options object.
29 *
30 * @param {Object} [opts] option object
31 */
32 function parseAuthOptions (opts) {
33 var matches;
34 if (opts.auth) {
35 matches = opts.auth.match(/^(.+):(.+)$/);
36 if (matches) {
37 opts.username = matches[1];
38 opts.password = matches[2];
39 } else {
40 opts.username = opts.auth;
41 }
42 }
43 }
45 /**
46 * connect - connect to an MQTT broker.
47 *
48 * @param {String} [brokerUrl] - url of the broker, optional
49 * @param {Object} opts - see MqttClient#constructor
50 */
51 function connect (brokerUrl, opts) {
53 if (('object' === typeof brokerUrl) && !opts) {
54 opts = brokerUrl;
55 brokerUrl = null;
56 }
58 opts = opts || {};
60 if (brokerUrl) {
61 opts = xtend(url.parse(brokerUrl, true), opts);
62 opts.protocol = opts.protocol.replace(/\:$/, '');
63 }
65 // merge in the auth options if supplied
66 parseAuthOptions(opts);
68 // support clientId passed in the query string of the url
69 if (opts.query && 'string' === typeof opts.query.clientId) {
70 opts.clientId = opts.query.clientId;
71 }
73 if (opts.cert && opts.key) {
74 if (opts.protocol) {
75 if (-1 === ['mqtts', 'wss'].indexOf(opts.protocol)) {
76 /*
77 * jshint and eslint
78 * complains that break from default cannot be reached after throw
79 * it is a foced exit from a control structure
80 * maybe add a check after switch to see if it went through default
81 * and then throw the error
82 */
83 /*jshint -W027*/
84 /*eslint no-unreachable:1*/
85 switch (opts.protocol) {
86 case 'mqtt':
87 opts.protocol = 'mqtts';
88 break;
89 case 'ws':
90 opts.protocol = 'wss';
91 break;
92 default:
93 throw new Error('Unknown protocol for secure conenction: "' + opts.protocol + '"!');
94 break;
95 }
96 /*eslint no-unreachable:0*/
97 /*jshint +W027*/
98 }
99 } else {
100 // don't know what protocol he want to use, mqtts or wss
101 throw new Error('Missing secure protocol key');
102 }
103 }
105 if (!protocols[opts.protocol]) {
106 opts.protocol = protocolList.filter(function (key) {
107 return 'function' === typeof protocols[key];
108 })[0];
109 }
111 if (false === opts.clean && !opts.clientId) {
112 throw new Error('Missing clientId for unclean clients');
113 }
116 function wrapper (client) {
117 if (opts.servers) {
118 if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
119 client._reconnectCount = 0;
120 }
122 opts.host = opts.servers[client._reconnectCount].host;
123 opts.port = opts.servers[client._reconnectCount].port;
124 opts.hostname = opts.host;
126 client._reconnectCount++;
127 }
129 return protocols[opts.protocol](client, opts);
130 }
132 return new MqttClient(wrapper, opts);
133 }
135 module.exports = connect;
136 module.exports.connect = connect;