-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
187 lines (151 loc) · 4.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict'
const mqtt = require('mqtt');
class AsyncClient {
constructor (client) {
this._client = client;
}
set handleMessage (newHandler) {
this._client.handleMessage = newHandler;
}
get handleMessage () {
return this._client.handleMessage;
}
get connected () {
return this._client.connected;
}
get reconnecting () {
return this._client.reconnecting;
}
publish (...args) {
return new Promise((resolve, reject) => {
this._client.publish(...args, (err, result) => {
if (err) reject(err)
else resolve(result)
})
})
}
subscribe (...args) {
return new Promise((resolve, reject) => {
this._client.subscribe(...args, (err, result) => {
if (err) reject(err)
else resolve(result)
})
})
}
unsubscribe (...args) {
return new Promise((resolve, reject) => {
this._client.unsubscribe(...args, (err, result) => {
if (err) reject(err)
else resolve(result)
})
})
}
end (...args) {
return new Promise((resolve, reject) => {
this._client.end(...args, (err, result) => {
if (err) reject(err)
else resolve(result)
})
})
}
reconnect (...args) {
return this._client.reconnect(...args);
}
addListener (...args) {
return this._client.addListener(...args);
}
emit (...args) {
return this._client.emit(...args);
}
eventNames (...args) {
return this._client.eventNames(...args);
}
getLastMessageId (...args) {
return this._client.getLastMessageId(...args);
}
getMaxListeners (...args) {
return this._client.getMaxListeners(...args);
}
listenerCount (...args) {
return this._client.listenerCount(...args);
}
listeners (...args) {
return this._client.listeners(...args);
}
off (...args) {
return this._client.off(...args);
}
on (...args) {
return this._client.on(...args);
}
once (...args) {
return this._client.once(...args);
}
prependListener (...args) {
return this._client.prependListener(...args);
}
prependOnceListener (...args) {
return this._client.prependOnceListener(...args);
}
rawListeners (...args) {
return this._client.rawListeners(...args);
}
removeAllListeners (...args) {
return this._client.removeAllListeners(...args);
}
removeListener (...args) {
return this._client.removeListener(...args);
}
removeOutgoingMessage (...args) {
return this._client.removeOutgoingMessage(...args);
}
setMaxListeners (...args) {
return this._client.setMaxListeners(...args);
}
}
module.exports = {
connect (brokerURL, opts) {
const client = mqtt.connect(brokerURL, opts);
const asyncClient = new AsyncClient(client);
return asyncClient;
},
connectAsync (brokerURL, opts, allowRetries=true) {
const client = mqtt.connect(brokerURL, opts);
const asyncClient = new AsyncClient(client);
return new Promise((resolve, reject) => {
// Listeners added to client to trigger promise resolution
const promiseResolutionListeners = {
connect: (connack) => {
removePromiseResolutionListeners();
resolve(asyncClient); // Resolve on connect
},
end: () => {
removePromiseResolutionListeners();
resolve(asyncClient); // Resolve on end
},
error: (err) => {
removePromiseResolutionListeners();
client.end();
reject(err); // Reject on error
}
};
// If retries are not allowed, reject on close
if (false === allowRetries) {
promiseResolutionListeners.close = () => {
promiseResolutionListeners.error('Couldn\'t connect to server');
}
}
// Remove listeners added to client by this promise
function removePromiseResolutionListeners () {
Object.keys(promiseResolutionListeners).forEach((eventName) => {
client.removeListener(eventName, promiseResolutionListeners[eventName]);
});
};
// Add listeners to client
Object.keys(promiseResolutionListeners).forEach((eventName) => {
client.on(eventName, promiseResolutionListeners[eventName]);
});
});
},
AsyncClient
};