Skip to content

Commit

Permalink
fix browser pubsub (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmklix authored Oct 24, 2023
1 parent cf1e8d7 commit 94be0bd
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions samples/browser/pub_sub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class AWSCognitoCredentialsProvider extends auth.CredentialsProvider{
this.source_provider = new AWS.CognitoIdentityCredentials({
IdentityPoolId: options.IdentityPoolId
});
this.aws_credentials =
this.aws_credentials =
{
aws_region: options.Region,
aws_access_id : this.source_provider.accessKeyId,
Expand Down Expand Up @@ -80,7 +80,7 @@ async function connect_websocket(provider: auth.CredentialsProvider) {
return new Promise<mqtt.MqttClientConnection>((resolve, reject) => {
let config = iot.AwsIotMqttConnectionConfigBuilder.new_builder_for_websocket()
.with_clean_session(true)
.with_client_id(`pub_sub_sample(${new Date()})`)
.with_client_id("test-" + Math.floor(Math.random() * 100000000))
.with_endpoint(Settings.AWS_IOT_ENDPOINT)
.with_credential_provider(provider)
.with_use_websockets()
Expand Down Expand Up @@ -113,7 +113,7 @@ async function connect_websocket(provider: auth.CredentialsProvider) {
async function main() {
/** Set up the credentialsProvider */
const provider = new AWSCognitoCredentialsProvider({
IdentityPoolId: Settings.AWS_COGNITO_IDENTITY_POOL_ID,
IdentityPoolId: Settings.AWS_COGNITO_IDENTITY_POOL_ID,
Region: Settings.AWS_REGION});
/** Make sure the credential provider fetched before setup the connection */
await provider.refreshCredentialAsync();
Expand All @@ -123,25 +123,25 @@ async function main() {
.then((connection) => {
connection
.subscribe(
"/test/me/senpai",
"test/topic",
mqtt.QoS.AtLeastOnce,
(topic, payload, dup, qos, retain) => {
const decoder = new TextDecoder("utf8");
let message = decoder.decode(new Uint8Array(payload));
log(`Message received: topic=${topic} message=${message}`);
/** The sample is used to demo long-running web service.
/** The sample is used to demo long-running web service.
* Uncomment the following line to see how disconnect behaves.*/
// connection.disconnect();
}
)
.then((subscription) => {
log(`start publish`)
var msg_count = 0;
connection.publish(subscription.topic, `NOTICE ME {${msg_count}}`, subscription.qos);
connection.publish(subscription.topic, `Hello world! ${msg_count}`, subscription.qos);
/** The sample is used to demo long-running web service. The sample will keep publishing the message every minute.*/
setInterval( ()=>{
msg_count++;
const msg = `NOTICE ME {${msg_count}}`;
const msg = `Hello world! ${msg_count}`;
connection.publish(subscription.topic, msg, subscription.qos);
}, 60000);
});
Expand Down

0 comments on commit 94be0bd

Please sign in to comment.