React Native AWS Amplify PubSub错误:“ AMQJS0007E套接字错误:未定义”

时间:2019-07-15 13:49:35

标签: react-native aws-amplify aws-iot

我正在使用AWS Amplify连接到AWS IoT Core,并收到错误“ AMQJS0007E套接字错误:未定义”。我已经在博览会应用程序中集成了AWS Amplify。

错误详细信息

错误代码:7 errorMessage:“ AMQJS0007E套接字错误:未定义。” invocationContext:未定义

代码

import React from "react";
import { StyleSheet, Text, View } from "react-native";
import Amplify, { PubSub, Auth } from "aws-amplify";
import { AWSIoTProvider } from "@aws-amplify/pubsub/lib/Providers";

Amplify.configure({
  Auth: {
    identityPoolId: "ap-south-1:xx-xx-xx-xx-xx",
    region: "AP_SOUTH_1",
    userPoolId: "ap-south-1_xx",
    userPoolWebClientId: "xxx"
  }
});
Amplify.addPluggable(
  new AWSIoTProvider({
    aws_pubsub_region: "ap-south-1",
    aws_pubsub_endpoint: "wss://xx.iot.ap-south-1.amazonaws.com/mqtt"
  })
);
class App extends React.Component {
  async componentDidMount() {
    Auth.configure();
    const sub1 = PubSub.subscribe("$aws/things/xxx/shadow/update").subscribe({
      next: data => console.log("Message received", data),
      error: error => console.error(error),
      close: () => console.log("Done")
    });
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
});

export default App;

1 个答案:

答案 0 :(得分:0)

不知道您是否还需要答案,但我发现最终为我解决的是转到 IAM 控制台并找到如下所示的角色:arn:aws:iam::<accountID>:role/<amplify-project-identifier>-authRole

然后我向角色添加了 IoTDataAccess 策略,然后重试发布和订阅,它似乎起作用了。