基于 WSS 的 React 和 AWS IoT MQTT - "connection lost - will attempt reconnection in x seconds"

React & AWS IoT MQTT over WSS - "connection lost - will attempt reconnection in x seconds"

我无法通过 MQTT 将 websockets 与经过身份验证的 cognito 用户成功连接到 AWS IoT 设备。 我在 React 应用程序中使用 aws-iot-device-sdk。 尝试连接时,以下调试日志会打印到 Web 浏览器控制台:

canonical request: GET
/mqtt
X-Amz-Algorithm=<removed>
SignedHeaders=host
host:<removed>.iot.us-east-1.amazonaws.com

host
<removed>

index.js:102 hashed canonical request: <removed>

index.js:111 string to sign: <removed>
<removed>
20210618/us-east-1/<removed>

index.js:117 signing key: <removed>

index.js:125 signature: <removed>

index.js:137 url: wss://<removed>.iot.us-east-1.amazonaws.com/mqtt?X-Amz-Algorithm=<removed>
index.js:630 using websockets, will connect to 'wss://<removed>.iot.us-east-1.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=....
index.js:781 connection lost - will attempt reconnection in 128 seconds...

创建设备和连接的 JS 代码:

var device = awsIot.device({
    clientId     : <unique client id>,
    region       : 'us-east-1',
    debug        : true,
    host         : '<removed>.iot.us-east-1.amazonaws.com',
    protocol     : 'wss',
    accessKeyId  : accessKeyId,
    secretKey    : secretAccessKey,
    sessionToken : sessionToken
});

device.on('connect', function(err, data) {
    if (err) {
        console.log(`Connection Error: ${err}`);
        return;
    }
    console.log('connected');
})

经过身份验证的 IoT AWS 角色:

"Action": [
                "iot:Connect",
                "iot:Subscribe",
                "iot:Publish",
                "iot:Receive"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:<removed>:client/cognito_user_*",
                "arn:aws:iot:us-east-1:<removed>:topic*/resource/*"
            ]

我可以使用带有 MQTT.fx 的证书连接到 MQTT 代理,但我正在处理的应用程序需要经过身份验证的 cognito 用户才能连接到代理。任何见解将不胜感激。

解决了这个问题。

我遇到的问题是 Cognito 用户没有必要的证书来访问资源。