[英]Mqtt connection with React native
I am currently making an IoT App that I'm trying to connect to sensors using MQTT.我目前正在制作一个 IoT 应用程序,我正在尝试使用 MQTT 连接到传感器。 I use the react_native_mqtt package.我使用 react_native_mqtt package。 What I'm trying to achieve is to connect and get the data of topic sent by sensor.我想要实现的是连接并获取传感器发送的主题数据。 but the code unfortunately not working.但不幸的是代码不起作用。 so What should I update in the app.js file to test the MQTT connection?那么我应该在 app.js 文件中更新什么来测试 MQTT 连接?
Any help is appreciated.任何帮助表示赞赏。
import init from 'react-native-mqtt'
const host = '55.122.xx.xx'
const port = '1883'
const clientId = `mqtt_${Math.random().toString(16).slice(3)}`
const connectUrl = `mqtt://${host}:${port}`
init({
size: 10000,
defaultExpires: 1000 * 3600 * 24,
enableCache: true,
sync: {},
});
export default class TestScreen extends Component {
constructor(){
super();
this.onMessageArrived = this.onMessageArrived.bind(this)
this.onConnectionLost = this.onConnectionLost.bind(this)
const client = new Paho.MQTT.Client(connectUrl, port, clientId,);
client.onMessageArrived = this.onMessageArrived;
client.onConnectionLost = this.onConnectionLost;
client.connect({
onSuccess: this.onConnect,
useSSL: false ,
userName: 'User',
password: 'Pass',
onFailure: (e) => {console.log("here is the error" , e); }
});
this.state = {
message: [''],
client,
messageToSend:'',
isConnected: false,
};
}
onMessageArrived(entry) {
console.log("onMessageArrived:"+message.payloadString);
this.setState({message: [...this.state.message, entry.payloadString]});
}
onConnect = () => {
const { client } = this.state;
console.log("Connected!!!!");
client.subscribe('hello/world');
this.setState({isConnected: true, error: ''})
};
sendMessage(){
message = new Paho.MQTT.Message(this.state.messageToSend);
message.destinationName = "hello/world";
if(this.state.isConnected){
this.state.client.send(message);
}else{
this.connect(this.state.client)
.then(() => {
this.state.client.send(message);
this.setState({error: '', isConnected: true});
})
.catch((error)=> {
console.log(error);
this.setState({error: error});
});
}
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
this.setState({error: 'Lost Connection', isConnected: false});
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native MQTT!
</Text>
<Text style={styles.instructions}>
Message: {this.state.message.join(' --- ')}
</Text>
<Text style={{color: 'red'}}>
{this.state.error}
</Text>
{ this.state.isConnected ?
<Text style={{color: 'green'}}>
Connected
</Text> : null
}
<TextInput
value={this.state.messageToSend}
onChangeText={(value => this.setState({messageToSend: value}))}
placeholder="Type hereee..."
style={styles.input} />
<Button onPress={this.sendMessage.bind(this) } title="Send Message" />
</View>
);
}
}
Error:错误:
TypeError: (0, _reactNativeMqtt.default) is not a function. (In '(0, _reactNativeMqtt.default)({
size: 10000,
defaultExpires: 1000 * 3600 * 24,
enableCache: true,
sync: {}
})', '(0, _reactNativeMqtt.default)' is an instance of Object)
This will not offer you a solution, just some hints which may or may not be helpful, but it's just too long to place in a comment.这不会为您提供解决方案,只是一些可能有用也可能没有帮助的提示,但它太长了,无法放在评论中。
This line: const client = new Paho.MQTT.Client(connectUrl, port, clientId,);
这一行: const client = new Paho.MQTT.Client(connectUrl, port, clientId,);
I don't see Paho
defined anywhere.我没有在任何地方看到Paho
定义。 You import MQTT
.您导入MQTT
。
You refer to another answer, but that was using a different MQTT library -- react_native_mqtt , and you're using react-native-paho-mqtt您参考了另一个答案,但那是使用不同的 MQTT 库—— react_native_mqtt ,而您正在使用react-native-paho-mqtt
The Client
constructor is documented to take 4 arguments, and you supply 3 and there's a dangling comma. Client
构造函数被记录为采用 4 arguments,并且您提供 3 并且有一个悬空的逗号。 Was it a typo?是错字吗?
You can find perhaps a better example of client instantiation on this npm page .您可以在此npm 页面上找到一个更好的客户端实例化示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.