Node.js 本机MQTT侦听器

Node.js 本机MQTT侦听器,node.js,react-native,Node.js,React Native,我有以下代码摘录: client.on ('message', function (msg) { // .... }); 有人能告诉我如何从这个函数中获取msg的值,以及如何从外部访问它吗。 我试过了,但没用: this.setState ({msg: msg}) 您可以制作一个MQTT管理器并使用它 示例 module.exports={//cached singleton实例 QOS:1,//兔子只支持0和1 道具:空, 创建(userID,connectionProps={}){ if

我有以下代码摘录:

client.on ('message', function (msg) {
// ....
});
有人能告诉我如何从这个函数中获取msg的值,以及如何从外部访问它吗。 我试过了,但没用:

this.setState ({msg: msg})

您可以制作一个
MQTT管理器
并使用它

示例

module.exports={//cached singleton实例
QOS:1,//兔子只支持0和1
道具:空,
创建(userID,connectionProps={}){
if(userID&&connectionProps){
// http://www.hivemq.com/demos/websocket-client/
this.onConnectionOpened=this.onConnectionOpened.bind(this);
this.onConnectionClosed=this.onConnectionClosed.bind(this);
this.onError=this.onError.bind(this);
this.onMessageArrived=this.onMessageArrived.bind(this);
this.disconnect=this.disconnect.bind(this);
const deviceId=this.randIdCreator()
.更换(/[^a-zA-Z0-9]+//g');
this.conProps=\ uu0.extend({
clientId:`realtime.${userID}.${deviceId}`,
channelToUse:`mqtt订阅实时。${userID}`,
作者:错,
clean:true,//clean session YES在所有客户端断开连接时删除队列
},连接道具);
/*创建mqtt客户机*/
MQTT.createClient(this.conProps)
。然后((客户端)=>{
this.client=client;
client.on('closed',this.onConnectionClosed);
client.on('error',this.onError);
client.on('message',this.onmessage驱动);
client.on('connect',this.onconnection已打开);
client.connect();
}).catch((错误)=>{
错误(`MQTT.createtClient错误:${err}`);
});
}
},
...
onMessageArrived(消息){
如果(信息){
log(`MQTT新消息:${JSON.stringify(消息)}`)
}
}
...
用法

从“/realtimeManager”导入MqttNotificationsManager;
//初始化实时
MqttNotificationsManager.create(
“鲍勃”,
{
uri:'mqtt://test.mosquitto.org:1883',
},
);