यहां एक नमूना है जो सदस्यता में कनेक्ट, प्रकाशित और प्रतिक्रिया करने के लिए जेएस में एक कॉग्निटो पहचान पूल का उपयोग करता है।
// Configure Cognito identity pool
AWS.config.region = 'us-east-1';
var credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:your identity pool guid',
});
// Getting AWS creds from Cognito is async, so we need to drive the rest of the mqtt client initialization in a callback
credentials.get(function(err) {
if(err) {
console.log(err);
return;
}
var requestUrl = SigV4Utils.getSignedUrl('wss', 'data.iot.us-east-1.amazonaws.com', '/mqtt',
'iotdevicegateway', 'us-east-1',
credentials.accessKeyId, credentials.secretAccessKey, credentials.sessionToken);
initClient(requestUrl);
});
function init() {
// do setup stuff
}
// Connect the client, subscribe to the drawing topic, and publish a "hey I connected" message
function initClient(requestUrl) {
var clientId = String(Math.random()).replace('.', '');
var client = new Paho.MQTT.Client(requestUrl, clientId);
var connectOptions = {
onSuccess: function() {
console.log('connected');
// subscribe to the drawing
client.subscribe("your/mqtt/topic");
// publish a lifecycle event
message = new Paho.MQTT.Message('{"id":"' + credentials.identityId + '"}');
message.destinationName = 'your/mqtt/topic';
console.log(message);
client.send(message);
},
useSSL: true,
timeout: 3,
mqttVersion: 4,
onFailure: function() {
console.error('connect failed');
}
};
client.connect(connectOptions);
client.onMessageArrived = function (message) {
try {
console.log("msg arrived: " + message.payloadString);
} catch (e) {
console.log("error! " + e);
}
};
}
Documentation for the credentials.get
call, here
सदस्यता लेने/प्रकाशन के साथ ही के लिए अपने IAM भूमिका को अधिकृत करने के लिए याद रखें।
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iot:Receive",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iot:Subscribe",
"Resource": [
"arn:aws:iot:us-east-1::your/mqtt/topic"
]
},
{
"Effect": "Allow",
"Action": "iot:Publish",
"Resource": [
"arn:aws:iot:us-east-1::your/mqtt/topic"
]
}
]
}
स्रोत
2016-02-17 12:14:06
sigv4 फ़ंक्शन [यहां] (http://draw.kyleroche.com/sigv4utils.js) संदर्भ के लिए है। –
मुझे निम्न त्रुटि मिल रही है: aws-sdk-2.7.1.js: 6834 असीमित त्रुटि: पल परिभाषित नहीं किया गया है (...) callListeners @ aws-sdk-2.7.1.js: 6834emit @ aws-sdk-2.7.1 .js: 6810emit @ aws-sdk-2.7.1.js: 4054transition @ aws-sdk-2.7.1.js: 3831runTo @ aws-sdk-p. जैसा कि मैंने aws sdk को भी अद्यतन किया है, लेकिन अभी भी वही त्रुटि मिल रही है जिससे आप मेरी मदद कर सकते हैं या यदि संभव हो तो aws-sdk.js फ़ाइल साझा करें जिसका आप उपयोग कर रहे हैं। –
आपने "iotdevicegateway" का उपयोग किया है, आप "iotdevicegateway" के रूप में पैरामीटर के रूप में क्या गुजर रहे हैं? या तो हमें संसाधन आईडी या डिवाइसआईडी पास करना होगा? –