内容简介:我们正在尝试使用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浏览器发送/接收消息》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 关于在接收POST请求,Tomcat偶发性接收到的参数不全问题排查分析
- 异步接收MSMQ消息
- 如何突破商品期货Tick接收限制
- SpringMVC接收和响应json数据
- 如何使用 jq 接收 blob 数据
- 转的关于公众号接收信息的返回
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTML Dog
Patrick Griffiths / New Riders Press / 2006-11-22 / USD 49.99
For readers who want to design Web pages that load quickly, are easy to update, accessible to all, work on all browsers and can be quickly adapted to different media, this comprehensive guide represen......一起来看看 《HTML Dog》 这本书的介绍吧!