应用程序是由angular开发的。
首先,用户应该使用Cognito登录。因此,在用户登录后,应用程序将获得CognitoUser数据,如其id令牌、访问密钥和会话令牌。
然后应用程序将开始连接到Iot Core,并尝试订阅或发布数据到我想要的主题。但我总是遇到Mqtt立即断开连接的情况。
以下是我的连接代码:
export class MqttService{
private awsIot: any;
private iotDevice: any;
public sendMessageSubject = new Subject<any>();
public receiveMessageSubject = new Subject<string>();
constructor(private authService: AuthService, private router: Router) {
this.awsIot = require('aws-iot-device-sdk');
AWS.config.region = 'us-east-2';
this.authService.getUserPool.getCurrentUser().getSession(function(err, session) {
if (err) {
console.log(err);
return;
} else if(session.isValid()) {
this.initMqtt(session.getIdToken().getJwtToken());
}
});
}
public sendMesssage(value: string): Observable<boolean> {
const topic = 'topic/topic1';
const res = this.iotDevice.publish(topic, JSON.stringify({'message': value}));
this.sendMessageSubject.next(res);
}
private initMqtt(idToken: string) {
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: '<< IDENTITY-POOL-ID >>',
Logins: {
'cognito-idp.us-east-2.amazonaws.com/us-east-2_XXXXXXXXX': idToken
}
});
(<AWS.CognitoIdentityCredentials> AWS.config.credentials).refresh((error) => {
if (error) {
console.log(error);
} else {
console.log('success');
const deviceOptions = {
clientId: '<< CLIENT-ID >>',
host: 'XXXXXXXXXXXXXX-ats.iot.us-east-2.amazonaws.com',
protocol: 'wss',
port: 443,
accessKeyId: AWS.config.credentials.accessKeyId,
secretKey: AWS.config.credentials.secretAccessKey,
sessionToken: AWS.config.credentials.sessionToken,
reconnectPeriod: 0
}
this.iotDevice = this.awsIot.device(deviceOptions);
this.iotDevice.on('error', (err) => {
console.log('MQTT Error');
});
this.iotDevice.on('connect', (result) => {
/* It is always triggered here, then go to 'close' scope */
console.log('MQTT connected');
this.iotDevice.subscribe('topic/topic1');
});
this.iotDevice.on('reconnect', () => {
console.log('MQTT reconnect');
});
this.iotDevice.on('close', () => {
/* It is always triggered immediately after trigger 'connect' */
this.iotDevice.end();
console.log('MQTT Disconnected');
});
this.iotDevice.on('message', (sourceTopic: string, payload: any) => {
console.log('Message Received from topic:' + sourceTopic);
console.log('Message content:' + payload.toString());
this.receiveMessageSubject.next(payload.toString());
});
}
});
}
}
在运行代码之前,我还运行以下命令:
aws iot附加-主体-策略-策略-名称"
我还将物联网策略附加到身份验证角色。
我认为主要问题在于
'cognito-idp.us-east-2.amazonaws.com/us-east-2_XXXXXXXXX': idToken
,但我不确定。
如果有人知道,请告诉我。谢谢
我在Iot Core中修改了策略,现在可以正常工作了。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
}
]
}
原件:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Connect",
"Resource": "arn:aws:iot:us-east-2:xxxxxxxxxxxx:client/<< CLIENT-ID >>"
},
{
"Effect": "Allow",
"Action": "iot:Publish",
"Resource": "arn:aws:iot:us-east-2:xxxxxxxxxxxx:topic/topoc1/*"
},
{
"Effect": "Allow",
"Action": "iot:Subscribe",
"Resource": "arn:aws:iot:us-east-2:xxxxxxxxxxxx:topic/topic1/*"
}
]
}
我在Eclipse中使用m2e插件。我创建了一个Maven项目,并在POM中添加了dropwizard-core依赖项,如http://dropwizard.codahale.com/getting-started/. 但是,我不能导出特定的dropwizard-core类。例如,当我 导入com.yammer.dropwizard.config.Configuration; 我收到一个错误,指出
我在AWS控制台上创建了一个MySQL RDS。然后我使用以下步骤在MySQL Workbench中成功连接到该RDS: 连接方式:标准(TCP/IP) 然后我尝试在我的Asp中连接它。net核心应用程序,它在openConnection()上给了我错误。使用了以下连接字符串: “Server=zargham.ccir1327bjhl.us-east-2.rds.amazonaws.com;Por
建立mqtt连接。 请求方式: "|4|1|1|host|port|iotid|iotpwd|\r" 参数: host 物联网连接host port mqtt连接的端口 iotid 物联网账号 iotpwd 物联网账号密码 返回值: "|4|1|1|1|\r" mqtt连接状态:连接成功 "|4|1|1|2|reason|\r" mqtt连接状态:连接失败,字符串reason表示失败的原因 Ard
我已经用MVC创建了一个ASP.NET核心Web。我用Identity搭建了它,并添加了我的默认布局页面和使用了登录页面。现在我遇到了一个问题,当我登录时,它找不到列“username”和“password”,我相信。我不是很确定,我会承认我能得到的所有帮助:)。这是我的网站上的错误: 而错误页面中红色部分为: 事先谢谢!
我一直在考虑MQTT协议,但我不想让一个外部服务器运行MQTT代理,而且我找不到Win IoT的代理。Windows IOT Core是否存在MQTT代理?如果没有,你会推荐什么通信协议?
下面是我在代码中所做的: keystore中的证书没有读取并给我IoException。