Last active
October 6, 2021 00:38
-
-
Save ijin/4c676538f9ca72c9362c3cf6d5e26b6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Refresh Sample</title> | |
<script src="aws-iot-sdk-browser-bundle.js"></script> | |
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.283.1.min.js"></script> | |
</head> | |
<body> | |
<h1 id="time">DATETIME</h1> | |
<span id="status"></span> | |
<script> | |
var now = new Date(); | |
document.getElementById("time").innerHTML = now.toLocaleTimeString(); | |
var awsIot = require('aws-iot-device-sdk'); | |
const PoolId = 'ap-northeast-1:1ab1b0e1-a523-4bef-b39c-c8daa51b6ccc'; | |
const region = 'ap-northeast-1'; | |
AWS.config.region = region; | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ | |
IdentityPoolId: PoolId | |
}); | |
const topic = "Refresh_Topic"; | |
const endpoint = 'a25xf946jh1j8i-ats.iot.ap-northeast-1.amazonaws.com'; | |
const clientId = 'client_id' + (Math.floor((Math.random() * 100000) + 1)); | |
const mqttClient = awsIot.device({ | |
region: region, | |
clientId: clientId, | |
host: endpoint, | |
port: 443, | |
protocol: 'wss', | |
maximumReconnectTimeMs: 8000, | |
debug: true, | |
accessKeyId: '', | |
secretKey: '', | |
sessionToken: '' | |
}); | |
var cognitoIdentity = new AWS.CognitoIdentity(); | |
function getCredentials() { | |
AWS.config.credentials.get(function(err, data) { | |
if (!err) { | |
console.log('retrieved identity: ' + AWS.config.credentials.identityId); | |
var params = { | |
IdentityId: AWS.config.credentials.identityId | |
}; | |
cognitoIdentity.getCredentialsForIdentity(params, function(err, data) { | |
if (!err) { | |
mqttClient.updateWebSocketCredentials( | |
data.Credentials.AccessKeyId, | |
data.Credentials.SecretKey, | |
data.Credentials.SessionToken); | |
} else { | |
console.log('error retrieving credentials: ' + err); | |
alert('error retrieving credentials: ' + err); | |
} | |
}); | |
} else { | |
console.log('error retrieving identity:' + err); | |
alert('error retrieving identity: ' + err); | |
} | |
}); | |
} | |
window.mqttClientConnectHandler = function() { | |
console.log('connect'); | |
mqttClient.subscribe(topic); | |
document.getElementById("status").innerHTML = "connect"; | |
}; | |
window.mqttClientReconnectHandler = function() { | |
console.log('reconnect'); | |
getCredentials(); | |
document.getElementById("status").innerHTML = "reconnect"; | |
}; | |
window.isUndefined = function(value) { | |
return typeof value === 'undefined' || value === null; | |
}; | |
window.mqttClientMessageHandler = function(topic, payload) { | |
console.log('message: ' + topic + ':' + payload.toString()); | |
var now = new Date(); | |
document.getElementById("time").innerHTML = now.toLocaleTimeString(); | |
}; | |
window.updatePublishTopic = function() {}; | |
mqttClient.on('connect', window.mqttClientConnectHandler); | |
mqttClient.on('reconnect', window.mqttClientReconnectHandler); | |
mqttClient.on('message', window.mqttClientMessageHandler); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment