gpt4 book ai didi

android - 如何从 onMessageArrived 检索 payloadString 到构造函数?

转载 作者:行者123 更新时间:2023-11-30 05:09:19 24 4
gpt4 key购买 nike

请检查代码。我想从 onMessageArrived 检索 payloadString 数据到构造函数。怎么做?基本上,我想将所有 JSON 数据存储在 Asyncstorage 中,并且还想在构造函数中应用一些逻辑。这就是为什么我想从 onMessageArrived 检索数据到构造函数。提前致谢。

import React, { Component } from 'react';
import init from 'react_native_mqtt';
import { AsyncStorage, StyleSheet, Text, View } from 'react-native';

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



export default class MqttLog extends Component {
constructor(props) {
super(props);

const client = new Paho.MQTT.Client('iot.eclipse.org', 443, 'uname');
client.onConnectionLost = this.onConnectionLost;
client.onMessageArrived = this.onMessageArrived;
client.connect({ onSuccess: this.onConnect, useSSL: true });


this.state = {
text: ['ELOGIC'],
client,
};

this.onMessageArrived.bind(this);
}



onConnect = () => {
const { client } = this.state;
client.subscribe('WORLD');
console.log('connect');
};

onConnectionLost = responseObject => {
if (responseObject.errorCode !== 0) {
var connectionLostMessage = `connection lost: ${responseObject.errorMessage}`;
console.log(connectionLostMessage);

}
};

onMessageArrived = message => {

var msg = message.payloadString;
var messageResult = `${msg}`;
console.log(messageResult);

};

render() {


return (
<View style={styles.container}>
<Text>Hello</Text>
</View>
);
}
}


const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
},
});

最佳答案

可以通过使用异步存储来解决这个问题。当数据来自 mqtt 时,我立即将数据推送到异步存储中,然后在构造函数中使用键值对从异步存储中检索。

关于android - 如何从 onMessageArrived 检索 payloadString 到构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53986488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com