1 | 'use strict'
|
2 |
|
3 | var MqttClient = require('../client')
|
4 | var Store = require('../store')
|
5 | var url = require('url')
|
6 | var xtend = require('xtend')
|
7 | var protocols = {}
|
8 |
|
9 | if (process.title !== 'browser') {
|
10 | protocols.mqtt = require('./tcp')
|
11 | protocols.tcp = require('./tcp')
|
12 | protocols.ssl = require('./tls')
|
13 | protocols.tls = require('./tls')
|
14 | protocols.mqtts = require('./tls')
|
15 | } else {
|
16 | protocols.wx = require('./wx')
|
17 | protocols.wxs = require('./wx')
|
18 | }
|
19 |
|
20 | protocols.ws = require('./ws')
|
21 | protocols.wss = require('./ws')
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function parseAuthOptions (opts) {
|
29 | var matches
|
30 | if (opts.auth) {
|
31 | matches = opts.auth.match(/^(.+):(.+)$/)
|
32 | if (matches) {
|
33 | opts.username = matches[1]
|
34 | opts.password = matches[2]
|
35 | } else {
|
36 | opts.username = opts.auth
|
37 | }
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function connect (brokerUrl, opts) {
|
48 | if ((typeof brokerUrl === 'object') && !opts) {
|
49 | opts = brokerUrl
|
50 | brokerUrl = null
|
51 | }
|
52 |
|
53 | opts = opts || {}
|
54 |
|
55 | if (brokerUrl) {
|
56 | var parsed = url.parse(brokerUrl, true)
|
57 | if (parsed.port != null) {
|
58 | parsed.port = Number(parsed.port)
|
59 | }
|
60 |
|
61 | opts = xtend(parsed, opts)
|
62 |
|
63 | if (opts.protocol === null) {
|
64 | throw new Error('Missing protocol')
|
65 | }
|
66 | opts.protocol = opts.protocol.replace(/:$/, '')
|
67 | }
|
68 |
|
69 |
|
70 | parseAuthOptions(opts)
|
71 |
|
72 |
|
73 | if (opts.query && typeof opts.query.clientId === 'string') {
|
74 | opts.clientId = opts.query.clientId
|
75 | }
|
76 |
|
77 | if (opts.cert && opts.key) {
|
78 | if (opts.protocol) {
|
79 | if (['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) === -1) {
|
80 | switch (opts.protocol) {
|
81 | case 'mqtt':
|
82 | opts.protocol = 'mqtts'
|
83 | break
|
84 | case 'ws':
|
85 | opts.protocol = 'wss'
|
86 | break
|
87 | case 'wx':
|
88 | opts.protocol = 'wxs'
|
89 | break
|
90 | default:
|
91 | throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
|
92 | }
|
93 | }
|
94 | } else {
|
95 |
|
96 | throw new Error('Missing secure protocol key')
|
97 | }
|
98 | }
|
99 |
|
100 | if (!protocols[opts.protocol]) {
|
101 | var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
102 | opts.protocol = [
|
103 | 'mqtt',
|
104 | 'mqtts',
|
105 | 'ws',
|
106 | 'wss',
|
107 | 'wx',
|
108 | 'wxs'
|
109 | ].filter(function (key, index) {
|
110 | if (isSecure && index % 2 === 0) {
|
111 |
|
112 | return false
|
113 | }
|
114 | return (typeof protocols[key] === 'function')
|
115 | })[0]
|
116 | }
|
117 |
|
118 | if (opts.clean === false && !opts.clientId) {
|
119 | throw new Error('Missing clientId for unclean clients')
|
120 | }
|
121 |
|
122 | function wrapper (client) {
|
123 | if (opts.servers) {
|
124 | if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
|
125 | client._reconnectCount = 0
|
126 | }
|
127 |
|
128 | opts.host = opts.servers[client._reconnectCount].host
|
129 | opts.port = opts.servers[client._reconnectCount].port
|
130 | opts.hostname = opts.host
|
131 |
|
132 | client._reconnectCount++
|
133 | }
|
134 |
|
135 | return protocols[opts.protocol](client, opts)
|
136 | }
|
137 |
|
138 | return new MqttClient(wrapper, opts)
|
139 | }
|
140 |
|
141 | module.exports = connect
|
142 | module.exports.connect = connect
|
143 | module.exports.MqttClient = MqttClient
|
144 | module.exports.Store = Store
|