AWS IOT JS设备SDK无法连接并不断重新连接

时间:2017-10-05 09:44:15

标签: javascript node.js aws-iot

var awsIot = require('aws-iot-device-sdk');
var uuid = require('uuid');
var device = awsIot.device({
    keyPath: "<path>-private.pem.key",
    certPath: "<path>-certificate.pem.crt",
    caPath: "<path>-root-CA.key",
    clientId: uuid.v4(),
    debug:true,
    host: <hostId>,
    will:{
        topic:"blahblahblah",
        payload:"disconnecting",
        qos:0,
        retain:false
    }
});

device
    .on('connect', function() {
        console.log('connect');
        device.subscribe('cgw/devices/register');
    });

device.on('message', (topic, payload, message) => {
    console.log(topic);
    console.log(payload);
    console.log(message);
});

   device
      .on('close', function() {
         console.log('close');
      });
   device
      .on('reconnect', function() {
         console.log('reconnect');
      });
   device
      .on('offline', function() {
         console.log('offline');
      });
   device
      .on('error', function(error) {
         console.log('error', error);
      });

我正在尝试运行上面的示例代码,但设备没有连接。我也试过传递

  

将param保留为false

根据https://github.com/aws/aws-iot-device-sdk-js/issues/61

但程序仍未运行并显示

离线 关 重新连接 关 重新连接 关 重新连接 关 重新连接

1 个答案:

答案 0 :(得分:0)

分配给身份/设备的策略似乎没有提供对 AWS IoT 代理的正确访问。因此,为了连接、发布、订阅和接收来自 AWS 代理的数据,相应的策略应包含以下语句:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": [
        "PUT YOU CLIENT RESOURCE HERE"
      ]
    },
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": [
        "PUT YOUR TOPIC RESOURCE HERE"
      ]
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": [
        "PUT YOUR TOPIC/FILTER RESOURCE HERE"
      ]
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": [
        "PUT YOUR TOPIC RESOURCE HERE"
      ]
    }
  ]
}