Can not connect to AWS Amplify PubSub -> Socket error:undefined

Can not connect to AWS Amplify PubSub -> Socket error:undefined

我一直在尝试所有方法让 Amplify/PubSub 正常工作,但没有任何运气。似乎所有的文档都已经过时了。

这是我到目前为止所做的。请注意,所有哈希值都是虚构的 ;-)

  1. 创建了一个全新的 React Native 应用程序
  2. 已安装放大包
  3. 已安装 Amplify CLI
  4. 已调用$ amplify configure
  5. 已调用$ amplify init
  6. 已调用$ amplify add auth
  7. 调用了 $ amplify push,它创建了 aws-exports.js 对象
  8. 创建了一个超级简单的组件
    import React from 'react';
    import { View } from 'react-native';

    import { withAuthenticator } from 'aws-amplify-react-native';
    import Amplify, { Auth, PubSub } from 'aws-amplify';
    import { AWSIoTProvider } from '@aws-amplify/pubsub/lib/Providers';

    import awsmobile from './aws-exports';

    Amplify.configure({
      Auth: {
        mandatorySignIn: true,
        region: awsmobile.aws_cognito_region,
        userPoolId: awsmobile.aws_user_pools_id,
        identityPoolId: awsmobile.aws_cognito_identity_pool_id,
        userPoolWebClientId: awsmobile.aws_user_pools_web_client_id,
      },
      Analytics: {
        disabled: true,
      },
    });

    Amplify.addPluggable(
      new AWSIoTProvider({
        aws_pubsub_region: 'ap-southeast-2',
        aws_pubsub_endpoint:
      'wss://a123456789d-ats.iot.ap-southeast-2.amazonaws.com/mqtt',
      })
    );

    Amplify.Logger.LOG_LEVEL = 'DEBUG';

    class App extends PureComponent {
      componentDidMount() {
        if (this.props.authState === 'signedIn') {
          Auth.currentCredentials().then((creds) => {

            // get the principal that needs to be attached to policy
            console.log('principal to be attached', creds.identityId)

            PubSub.subscribe('topic_1').subscribe({
              next: (data) => console.log(JSON.stringify(data, null, 2)),
              error: (msg) => console.log('ERROR: ', msg.error),
              close: () => console.log('Done'),
            });
          });
        }
      }

      render() {
        return (
          <View/>
        );
      }
    }

    export default withAuthenticator(App);
  1. 我将 AWS 根证书附加到我的 iPhone(见下文)

10.Create AWS IoT 的 IAM 策略

11.Attach 我从 Auth.currentCredentials 拿到的 principal

aws iot attach-principal-policy --policy-name IoTAppPolicy --principal ap-southeast-2:db1234bc-5678-90123-4567-89ae0e123b4

12.Attach 政策Auth Role

然而,当我 运行 应用程序时,我收到以下错误日志

[DEBUG] 51:42.745 SignIn - Sign In for test@test.com
[DEBUG] 51:44.616 AuthClass CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 0}
[DEBUG] 51:44.616 Credentials - set credentials from session
[DEBUG] 51:46.247 Credentials - Load credentials successfully 
[DEBUG] 51:46.248 AuthClass - succeed to get cognito credentials 
DEBUG] 51:47.150 Hub - Dispatching to auth with  {event: "signIn", data: CognitoUser, message: "A user 2d...de has been signed in"}
[DEBUG] 51:47.151 SignIn CognitoUser {username: "2d...de", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession, …}
[DEBUG] 51:47.152 AuthClass - Getting the session from this user: CognitoUser {username: "2d...de", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession, …}
[DEBUG] 51:47.152 AuthClass - Succeed to get the user session CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 1}
[DEBUG] 51:47.574 AuthPiece - verified user attributes {verified: {…}, unverified: {…}}
[INFO] 51:47.575 Authenticator - Inside handleStateChange method current authState: signIn
[DEBUG] 51:47.575 VerifyContact - no unverified contact
[INFO] 51:47.578 Authenticator - authState has been updated to signedIn
[DEBUG] 51:47.581 AuthClass - getting current credentials
[DEBUG] 51:47.582 Credentials - getting credentials
[DEBUG] 51:47.582 Credentials - picking up credentials
[DEBUG] 51:47.582 Credentials - getting new cred promise
[DEBUG] 51:47.582 Credentials - checking if credentials exists and not expired
[DEBUG] 51:47.583 Credentials - credentials not changed and not expired, directly return
[DEBUG] 51:47.583 AnalyticsClass - Analytics has been disabled
[DEBUG] 51:47.584 PubSub - subscribe options undefined
[DEBUG] 51:47.584 MqttOverWSProvider - Subscribing to topic(s) topic1
[DEBUG] 51:47.584 Credentials - getting credentials
[DEBUG] 51:47.584 Credentials - picking up credentials
[DEBUG] 51:47.584 Credentials - getting new cred promise
[DEBUG] 51:47.585 Credentials - checking if credentials exists and not expired
[DEBUG] 51:47.585 Credentials - are these credentials expired?
[DEBUG] 51:47.585 Credentials - credentials not changed and not expired, directly return
[DEBUG] 51:47.586 Signer {region: "ap-southeast-2", service: "iotdevicegateway"}
[DEBUG] 51:47.590 MqttOverWSProvider - Creating new MQTT client cca4e07f-a15a-46ce-904d-483a83162018
[WARN] 52:50.152 MqttOverWSProvider - cca4e07f-a15a-46ce-904d-483a83162018 {
  "errorCode": 7,
  "errorMessage": "AMQJS0007E Socket error:undefined.",
  "uri": "wss://a123456789d.iot.ap-southeast-2.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAU7VGXF6UOWZIUFOM%2F20200502%2Fap-southeast-2%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20200502T055147Z&X-Amz-SignedHeaders=host&X-Amz-Signature=010d8..7dba&X-Amz-Security-Token=IQ..lk%3D"
}
ERROR: Disconnected, error code: 7
[DEBUG] 52:50.153 MqttOverWSProvider - Unsubscribing from topic(s) topic1

知道为什么我无法连接到主题吗?

谁能想到包裹不见了。

安装后

yarn add amazon-cognito-identity-js

我的身体一切正常 iPhone。

我去了 AWS IoT Core 页面,发布了 一条测试消息,瞧,我得到了以下 object 返回

{
  "message": "Hello from AWS IoT console"
}