【问题标题】:Connection failed - trying to connect to the mosquitto broker using React native连接失败 - 尝试使用 React native 连接到 mosquitto 代理
【发布时间】:2021-11-27 11:15:59
【问题描述】:

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

这里是错误对象 { "errorCode": 7, "errorMessage": “AMQJS0007E 套接字错误:未定义。”、“invocationContext”: 未定义,}

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: ''})
  };

【问题讨论】:

    标签: javascript react-native mqtt paho


    【解决方案1】:

    我们通过编辑 mosquitto 配置文件以添加新的侦听器端口 8883 并为该端口使用 websocket 协议来解决此问题 https://stackoverflow.com/a/32309525/12166187

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 2017-01-26
      • 2021-10-21
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多