node.js – 如何使用AWS IoT向/从Web浏览器发送/接收消息

栏目: Node.js · 发布时间: 6年前

内容简介:我们正在尝试使用Amazon Web Services物联网(AWS IoT)从Web浏览器发送消息(例如:鉴于AWS IoT支持JavaScript,我们预计这是可能的…我们在AWS IoT文档中搜索,但只找到服务器端示例(它暴露了AWS密钥/密钥…)是否有任何良好的工作示例或教程使用AWS IoT通过WebSockets / MQTT在浏览器中发送/接收消息(例如:使用AWS Cognito进行身份验证)?谢谢!

我们正在尝试使用Amazon Web Services物联网(AWS IoT)从Web浏览器发送消息(例如:鉴于AWS IoT支持JavaScript,我们预计这是可能的…

我们在AWS IoT文档中搜索,但只找到服务器端示例(它暴露了AWS密钥/密钥…)

是否有任何良好的工作示例或教程使用AWS IoT通过WebSockets / MQTT在浏览器中发送/接收消息(例如:使用AWS 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);
        }

    };
}

请记住,授权您的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"
            ]
        }
    ]
}

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/35439742/how-to-use-aws-iot-to-send-receive-messages-to-from-web-browser


以上所述就是小编给大家介绍的《node.js – 如何使用AWS IoT向/从Web浏览器发送/接收消息》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Effective Engineer

The Effective Engineer

Edmond Lau / The Effective Bookshelf, Palo Alto, CA. / 2015-3-19 / USD 39.00

Introducing The Effective Engineer — the only book designed specifically for today's software engineers, based on extensive interviews with engineering leaders at top tech companies, and packed with h......一起来看看 《The Effective Engineer》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具