amazon-web-services - AMQJS0008I 使用 AWS IOT 发布/订阅时套接字关闭
问题描述
首先,我尝试使用 AWS Amplify 库来使用 IOT 模块为我的无服务器应用程序创建 AWS IOT 聊天(我按照文档中给出的步骤进行操作),但它不起作用并给出了“Socket Already Closed”错误。
然后我尝试了 aws-iot-device-sdk 并按照 AWS 文档和代码片段这样做,但同样的错误。
我已按照以下步骤操作:
- 我已将“iot:*”权限授予我的 Cognito 池。
- 我创建了一个策略并将我的 Cognito 用户身份附加到该策略。
- 然后我尝试使用SDK和放大库连接它,它连接成功,但是当我尝试发布/订阅一个主题时,它抛出了同样的错误。
解决方案
我不确定您的问题可能出在哪里,但是当我第一次开始使用 Amplify PubSub 时,我遇到了同样的问题。对我来说,这是一个政策问题。因此,其中一件事可能会有所帮助:
将您的事物策略文档连接到联合池的用户身份(而不是来自用户池的身份)。我的政策文件如下所示:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:AttachPrincipalPolicy",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow"
],
"Resource": [
"*"
]
}
]
}
要为我的所有用户(不是很多)附加/更新策略,我使用 Lambda 函数来执行此操作:
var AWS = require("aws-sdk");
const cognito = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});
var cognitoidentity = new AWS.CognitoIdentity();
var iot = new AWS.Iot({apiVersion: '2015-05-28'});
exports.handler = (event, context, callback) => {
var params = {
IdentityPoolId: 'eu-central-1:xxxxxxxx-xxxx-xxxx-xxx-xxxxxxxx', /* change this */
MaxResults: 50,
};
cognitoidentity.listIdentities(params, function(err, data) {
if (err) console.log(err, err.stack);
else {
addPolicies(data.Identities);
}
});
function addPolicies(users) {
for (let i = 0; i<users.length;i++) {
var params2 = {
policyName: 'myIOTPolicy',
principal: users[i].IdentityId
};
iot.attachPrincipalPolicy(params2, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
}
}
callback(null, event);
};
对于身份池中经过身份验证的角色,我将以下策略附加到该角色:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:AttachPrincipalPolicy",
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow"
],
"Resource": [
"*"
]
}
]
}
希望这可以帮助
推荐阅读
- typescript - 在打字稿中定义至少一个集合的属性
- matlab - Matlab将标量函数扩展为矩阵函数
- raspberry-pi - 如何更改 helm 值以保持持久性以使用某个节点上的路径
- android - 基于 URI 的意图过滤器在 Chrome 隐身/Twitter IAB 中不起作用
- html - Django区分用户和模板条件
- sql - 将所有值从一个表中的列移动到新表,并使用这些表之间的关系更新第三个表,PostgreSQL
- python - 测试这个 UDP 套接字 Python 代码:奇怪的延迟;Windows 与 Linux
- mysql - 合并两个表中的行/列 - JOIN 子句
- c - 在 C 中访问结构的动态数组中的数组元素
- javascript - Vue电子邮件验证未返回true