为什么我的 Aws IoT 连接在 React-native 中永远挂起?

时间:2021-02-07 22:12:14

标签: android react-native mqtt aws-iot

  1. 我已经设置了 Aws IoT。
  2. 我有一个在 Android 模拟器中运行的 React Native 简单应用。
  3. 我已经实现了这个库 https://www.npmjs.com/package/react-native-native-mqtt
  4. 我的 pem、crt 和密钥存储在 %USERPROFILE%\certs 中。
  5. 我在 cmd mqtt pub --cafile %USERPROFILE%\certs\root-ca.pem --cert %USERPROFILE%\certs\certificate.pem.crt --key %USERPROFILE%\certs\private.pem.key -d -h ******-ats.iot.us-east-2.amazonaws.com -p 8883 -t test -m "hello there" 中运行此命令
  6. 我检查过。它出现在 Aws IoT 测试控制台中,我已订阅测试。

这是我的代码:

import { Buffer } from 'buffer';
var process = require("process");   // THIS LINE
var pna = require('process-nextick-args');
global.Buffer = Buffer;
const mqtt = require('mqtt')
const path = require('path');

const KEY = path.resolve(__dirname, './certs/private.pem.key');
const CERT = path.resolve(__dirname, './certs/certificate.pem.crt');
const CAfile = path.resolve(__dirname, './certs/root-ca.pem');

const options = {
  host: "a2b0xwckvjuafw-ats.iot.us-east-2.amazonaws.com",
  port: 8883,
  protocolId: 'MQTT',
  ca: CAfile,
  keyPath: KEY,
  certPath: CERT,
  // secureProtocol: 'TLSv1_method',
  clientId: 'rasppi4',
  protocolVersion: 4,
  will: {
    topic: 'WillMsg',
    payload: 'Connection Closed abnormally..!',
    qos: 0,
    retain: false
  },
};
const client  = mqtt.connect("wss://a2b0xwckvjuafw-ats.iot.us-east-2.amazonaws.com",options)

client.on('connect', function () {
  console.log("publish!!!!")
  client.publish('test', 'Hello mqtt')
  client.subscribe('test', function (err) {
    if (!err) {
      client.publish('test', 'Hello mqtt')
    }
  })
})

当我在 expo startweb 中运行 android

Screenshot of Websocket keep on pending

0 个答案:

没有答案
相关问题