首页 > 解决方案 > 如何使用 mqtt.js 连接到 AWS IoT

问题描述

以下代码报告握手超时错误。我没有在代码上提供确切的数据,而是根据我自己对这些参数的理解。我的理解有什么问题吗?

var options = {
    clientId: a random String,
    endpoint: the endpoint of AWS IoT Core,
    accessKey: I generate this from IAM - > Dashboard - > My access Key - > Access key,
    secretKey: I generate this from IAM - > Dashboard - > My access Key - > Access key,
    regionName: i find this from endpoint,
    debug: true
};


var client = mqtt.connect('ws://' + endpoint, options);
//var client  = mqtt.connect('mqtt://test.mosquitto.org');
client.on('connect', function() {
    alert("MQTT connected");
    client.subscribe('topic_1')
    client.publish('topic_1', 'Hello mqtt2')
})

client.on('message', function(topic, message) {
    // message is Buffer
    alert(message.toString())
    client.end()
})

我想知道我是否误解了 connect 函数的第一个参数,但我不知道如何更改。

我的政策如下所示。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:*",
      "Resource": "*"
    }
  ]
}

我确信我已将此政策附加到我的证书中。

标签: amazon-web-servicesmqttiot

解决方案


推荐阅读