25

我们正在尝试使用物联网的亚马逊网络服务的互联网(AWS IOT)从/发送消息到Web浏览器(例如:鉴于AWS物联网支持JavaScript,我们预计这是可能 ...如何使用AWS物联网发送从Web浏览器/接收消息/

我们已搜查在AWS物联网文档,但只发现服务器端的例子(其中暴露AWS秘密/键... )

是否有良好的工作例子或教程使用AWS物联网发送/然后通过WebSockets/MQTT在浏览器接收消息(例如:与AWS Cognito认证)?谢谢!

回答

19

下面是一个使用cognito身份池JS连接,发布和应对订阅样本。

// 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" 
      ] 
     } 
    ] 
} 
+7

sigv4函数是[here](http://draw.kyleroche.com/sigv4utils.js)以供参考。 –

+0

正在以下错误: 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 -..... 正如我已经更新了AWS SDK还,但仍然得到相同的错误,你可以帮我,或者如果可能的话,分享您正在使用的aws-sdk.js文件。 –

+0

你已经使用过“iotdevicegateway”,你是以“iotdevicegateway”作为参数传递的是什么?要么我们必须传递resourceId或DeviceId? –

1

万一别人是寻找一个解决方案:here's a tutorial是通过一个简单的聊天应用程序如何获得实时更新到使用无服务器和WebSockets的对AWS IOT一个ReactJS前端演示。本教程的源代码可用on Github

+1

本示例不包含Cognito支持。目前还不清楚:该示例是否在JavaScript源代码中显示键? –

相关问题