似乎有一个已知的错误。然而,这段代码真的帮助了我:
这是下面的链接和代码:
https ://github.com/Azure/azure-iot-sdk-node/issues/28
'use strict'
var token = "...";
var Client = require('azure-iot-device').Client;
var DeviceProtocol = require('azure-iot-device-mqtt').MqttWs; // Default transport for Receiver
var client = Client.fromSharedAccessSignature(token, DeviceProtocol);
var isDissconnected = true;
client.on('disconnect', function () {
isDissconnected = true;
console.log('Disconnected');
client.close(function () {
console.log('Closed');
});
});
client.on('connected', function () {
console.log('Connected');
});
client.on('error', function (err) {
console.error('ERROR: ' + err.message);
});
var connectClient = function () {
console.log('Connecting client');
client.open(function (err, transport) {
if (err) {
console.log('Could not connect: ' + err);
}
else {
isDissconnected = false;
client.getTwin(function (err, twin) {
if (err) {
console.log('Could not get twin: ' + err);
}
else {
console.log('Got twin');
}
});
}
});
}
connectClient();
setInterval(function () {
if (isDissconnected) {
console.log("Retrying to connect...");
connectClient();
}
}, 10000);