简体   繁体   English

如何在 React-Native 中连接到 MQTT 代理?

[英]How to connect to an MQTT broker in React-Native?

I am trying to integrate MQTT into my app.我正在尝试将 MQTT 集成到我的应用程序中。 I was reading this post , but I can't connect to the broker due to either using an outdated React Native MQTT library or my code being wrong.我正在阅读这篇文章,但由于使用过时的 React Native MQTT 库或我的代码错误,我无法连接到代理。 How can I successfully connect to a broker?如何成功连接到代理?

import init from 'react_native_mqtt';
import { AsyncStorage } from 'react-native';

init({
  size: 10000,
  storageBackend: AsyncStorage,
  defaultExpires: 1000 * 3600 * 24,
  enableCache: true,
  reconnect: true,
  sync : {
  }
});

const client = new Paho.MQTT.Client(
  "m23.cloudmqtt.com", 
  18628,
  "clientID-" + parseInt(Math.random() * 100)
);

client.connect({ 
  useSSL: true,
  userName: "username",
  password: "password"
});

function onConnect() {
  client.subscribe("topic");
}

SUPER LATE answer here but I'm hoping that this helps other because it took me a good 3 days to figure this out.在这里回答太晚了,但我希望这能帮助其他人,因为我花了 3 天的时间才弄明白。 The thing is that the library that you are using works as promised but it requires web sockets, I'm not saying that now you have to implement web-sockets but now you will have to use the Web-Socket Port for your broker.问题是您使用的库按承诺工作,但它需要网络套接字,我并不是说现在您必须实现网络套接字,但现在您必须为您的代理使用网络套接字端口。

For Example ::
Broker : broker.hivemq.com

Port :
1) 8000 (for web sockets)
2) 1883 (for TCP connection, pretty sure you'll be using this in your IOT)

So your client creation code will now look something like this ::所以你的客户端创建代码现在看起来像这样 ::


    const client = new Paho.MQTT.Client(
      "broker.hivemq.com", 
      8000,
      "clientID-" + parseInt(Math.random() * 100)
    );

Read more on the HiveMQ Public Broker and its Web-Sockets Browser Client so that makes it easy for you to test your MQTT publish and subscribe!阅读有关HiveMQ 公共代理及其Web 套接字浏览器客户端的更多信息,以便您轻松测试 MQTT 发布和订阅!

Not only that, but the AsyncStorage that you are using is deprecated .不仅如此,您使用的AsyncStorage已弃用 So use this instead.所以改用这个

With the new AsyncStorage and the Web-Sockets port for the broker, you are ready to go!有了新的 AsyncStorage 和代理的 Web-Sockets 端口,您就可以开始了! Or at least for the storage and connection part :)或者至少对于存储和连接部分:)

Here's my code, for better understanding ::这是我的代码,以便更好地理解::


    import AsyncStorage from '@react-native-async-storage/async-storage';
    import init from 'react_native_mqtt';
    
    init({
      size: 10000,
      storageBackend: AsyncStorage,
      defaultExpires: 1000 * 3600 * 24,
      enableCache: true,
      reconnect: true,
      sync : {}
    });
    
    function onConnect() {
      console.log("onConnect");
      client.subscribe('reactnative/testmqtt');
    }
    
    function onConnectionLost(responseObject) {
      if (responseObject.errorCode !== 0) {
        console.log("onConnectionLost:"+responseObject.errorMessage);
      }
    }
    
    function onMessageArrived(message) {
      var x = "\nTopic : "+message.topic+"\nMessage : "+message.payloadString;
      console.log(x);
    }
    
    const client = new Paho.MQTT.Client('broker.hivemq.com', 8000, 'uname');
    client.onMessageArrived = onMessageArrived;
    client.connect({ onSuccess:onConnect, useSSL: false });
    client.onConnectionLost = onConnectionLost;

PS Don't use SSL because that didn't work for some reason 0_o PS 不要使用 SSL,因为由于某种原因它不起作用 0_o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM