如何在 React-Native 中实现 AWS IoT(device)?

How to implement AWS IoT(device) in React-Native?

我正在尝试使用 React-Native 实施 AWS-IoT(设备)。

我用过包,

1) aws-iot-device-sdk-js

2) react-native-aws-iot-device-shadows

在使用这个包时出现了很多错误。我可以调试一些,但没有得到预期的结果。

我正在为聊天应用程序实施 AWS-IoT。

我正在使用 REST API 成功创建 IoT 会话并将这些作为响应 iotEndpoint, region, accessKey, secretKey, sessionToken。 但是使用这些凭据我无法使用上述软件包 Connect

我想通了,

步骤 1: 安装 aws-iot npm 包 npm install --save aws-sdk aws-iot-device-sdk

步骤 2: 安装 nodeifynpm install --save-dev rn-nodeify

第三步:运行这条命令安装指定的系列包

npx rn-nodeify --install "fs,util,path,tls,stream,buffer,global,process" --hack

"Please wait until the all the packages are installed"

第 4 步: 转到 package.json -> 在 scripts 部分添加,

"postinstall": "rn-nodeify --install fs,util,path,tls,stream,buffer,global,process --hack"

步骤 5: 安装 asyncstorage-downnpm install --save asyncstorage-down

第 6 步: rn-nodeify 将在您的 react- 根部分自动生成一个文件 shim.js本机项目。像这样将其导入 index.js 文件 import './shim'

最后,您可以使用 aws-iot 包了!!!

建议在后端使用 REST API 生成上述问题中指定的物联网会话密钥。

我已阅读此 post 和聊天记录,但一直无法找到解决此问题的方法,而我似乎也有。我已按照 Ron 此处描述的所有步骤进行操作,但出现 filesys.existsSync is not a function 错误。我已将 shim 导入作为 index.js 中的第一行代码。与AWS通信的代码如下。

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import AwsIot from 'aws-iot-device-sdk'

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {
  constructor(props){
    super(props)
    this.connectToIoT()
  }

  connectToIoT(){
    var device = AwsIot.device({
       keyPath: './cert/mykey-private.pem.key',
       certPath: '/cert/mycert-certificate.pem.crt',
       caPath:   './cert/AmazonRootCA1.pem.key',
       clientId: 'myclientid',
       host: 'myhost'
   });
   console.log(device)
   device
    .on('connect', function() {
      console.log('connect');
    });
    device
    .on('message', function(topic, payload) {
      console.log('message', topic, payload.toString());
    });
    }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

在使用证书进行身份验证时,是否有任何可能的解决方案或其他使用 MQTT 与 AWS IoT 通信的方法?