连接失败 - 尝试使用 React native 连接到 mosquitto 代理

Connection failed - trying to connect to the mosquitto broker using React native

我尝试将 MQTT 与 React Native 绑定。尝试连接到代理时,当我 运行 我的代码时,几分钟后我收到以下错误:

here is the error Object { "errorCode": 7, "errorMessage": "AMQJS0007E Socket error:undefined.", "invocationContext": undefined, }

import init from 'react_native_mqtt'
import AsyncStorage from '@react-native-async-storage/async-storage'

 init({
  size: 10000,
  storageBackend: AsyncStorage,
  defaultExpires: 1000 * 3600 * 24,
  enableCache: true,
  reconnect: true,
  sync : {
  }
});
 
   constructor(){
    super();
    this.onConnect = this.onConnect.bind(this)
    const client = new Paho.MQTT.Client('52.11.11.11', 1883, "clientId-" + parseInt(Math.random() * 100, 10));
    client.connect({ 
      onSuccess: this.onConnect,
      userName: "user",
      password: "pass",
      onFailure: (e) => {console.log("here is the error" , e); }

    });

    this.state = {
      message: [''],
      client,
      messageToSend:'',
      isConnected: false,
    };

  }

  onConnect = () => {
    const { client } = this.state;
    console.log("Connected!!!!");
    this.setState({isConnected: true, error: ''})
  };

我们通过编辑 mosquitto 配置文件来添加新的侦听器端口 8883 并为该端口使用 websocket 协议来解决这个问题