我是React Native的新手,我想在我的应用程序中使用MQTT。我试图按照this link中的说明操作。
我通过以下命令安装了MQTT:npm install react_native_mqtt --save
我的问题是:
- 我应该将下面的代码粘贴到哪里?
app.js
文件中?我需要创建另一个文件吗? - 我应该在
app.js
文件中写什么来测试MQTT连接?
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 : {
}
});
function onConnect() {
console.log("onConnect");
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
const client = new Paho.MQTT.Client('iot.eclipse.org', 443, 'uname');
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({ onSuccess:onConnect, useSSL: true });
字符串
1条答案
按热度按时间w8biq8rn1#
对于新的谷歌用户:例如,在你成功地安装了库之后,这可能是你的App.js:
字符串