Skip to content

Commit 0b6b464

Browse files
committed
fix disconnect procedures
1 parent c4c0a32 commit 0b6b464

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

arduino-iot-client-mqtt/arduino-iot-client-mqtt.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ class ArduinoClientMqtt {
179179
});
180180

181181
if (typeof this.opts.onDisconnect === 'function') {
182-
client.on("close", this.opts.onDisconnect);
182+
client.on("offline", this.opts.onDisconnect);
183+
client.on("disconnect", this.opts.onDisconnect);
183184
}
184185
});
185186
}
@@ -221,7 +222,7 @@ class ArduinoClientMqtt {
221222
try {
222223
if (this.connection) {
223224
// Disconnect to the connection that is using the old token
224-
this.connection.end();
225+
await this.connection.end();
225226

226227
// Remove the connection
227228
this.connection = null;

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"jws": "^3.2.2",
2626
"lodash": "^4.17.15",
2727
"moment": "^2.24.0",
28-
"mqtt": "^3.0.0"
28+
"mqtt": "^3.0.0",
29+
"request": "^2.88.0"
2930
}
3031
}

utils/arduino-connection-manager.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ async function getClientMqtt(connectionConfig){
6767
const ArduinoCloudOptions = {
6868
host: arduinoCloudHost,
6969
token: tokenInfo.token,
70-
onDisconnect: () => {
70+
onDisconnect: (clientId) => {
7171
// TODO: cosa vuol dire ?
72-
disconnected(clientId);
72+
//disconnected(clientId);
7373
console.log(`connection lost for ${clientId}`);
7474
},
7575
useCloudProtocolV2: true
@@ -192,11 +192,12 @@ async function updateToken(connectionConfig){
192192
}
193193

194194
async function deleteClientMqtt(clientId, thing, propertyName ){
195+
const releaseMutex = await getClientMutex.acquire();
195196
var user = findUser(clientId);
196197
if(user !== -1){
197198
if(connections[user].clientMqtt !== null){
198199
var ret = await connections[user].clientMqtt.removePropertyValueCallback(thing, propertyName);
199-
// TODO: NOT CLEAR WHAT FOLLOWS. SHOULD BE -1 INSTEAD OF 0 ?
200+
200201
if(ret === 0){
201202
await connections[user].clientMqtt.disconnect();
202203
delete connections[user].clientMqtt;
@@ -209,6 +210,7 @@ async function deleteClientMqtt(clientId, thing, propertyName ){
209210
}
210211
}
211212
}
213+
releaseMutex();
212214
}
213215

214216
function deleteClientHttp(clientId){

0 commit comments

Comments
 (0)