node.js和java之间的mqtt通信 [英] mqtt communication between node.js and java

查看:355
本文介绍了node.js和java之间的mqtt通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是使用mqtt协议发送数据。 Java项目(tempSensor)使用mqtt协议生成tempvalue,使用mqtt订阅tempvalue的node.js。 node.js和java项目都使用相同的密钥进行发布/订阅。我可以使用java项目发布数据,也可以在node.js中订阅数据。但数据不是可读格式。怎么做 ?因此数据是可读格式。
TempStruct的结构如下:

Goal is to send data using mqtt protocol. Java project (tempSensor) produce tempvalue using mqtt protocol and node.js which subscribe tempvalue using mqtt. Both node.js and java project use same key for publish/subscribe. I am able to publish data using java project and also subscribe data in node.js. But data is not in readable format. How to do it ? So that data is in readable format. Structure for TempStruct is as below:

public class TempStruct implements Serializable {
private static final long serialVersionUID = 1L;

private double tempValue;

public double gettempValue() {
    return tempValue;
}

private String unitOfMeasurement;

public String getunitOfMeasurement() {
    return unitOfMeasurement;
}

public TempStruct(double tempValue, String unitOfMeasurement) {

    this.tempValue = tempValue;
    this.unitOfMeasurement = unitOfMeasurement;
}

public String toJSON() {
      String json = String.format("{'tempValue': %f, 'unitOfMeasurement': '%s'}", tempValue, unitOfMeasurement);
      return json;
    }
   }

使用mqtt发布数据的代码如下:

Code which published data using mqtt is as below:

Logger.log(myDeviceInfo.getName(), "TemperatureSensor",
                "Publishing tempMeasurement");
        System.out.println("JSON Value...."+newValue.toJSON());
        try {
            this.myPubSubMiddleware.publish("tempMeasurement",
                    newValue.toJSON().getBytes("utf-8"), myDeviceInfo);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

使用mqtt接收数据的代码如下所示:(Node.js)

Code which received data using mqtt is shown below:(Node.js)

var mqtt=require('mqtt');
var client=mqtt.connect('mqtt://test.mosquitto.org:1883');
var data;
client.subscribe('tempMeasurement');
client.on('message',function(topic,payload){
//arg=JSON.stringify(arg);
console.log("In Message......");
var tempStruct = JSON.parse(payload);
console.log("tempValue: "+tempStruct.tempValue); 
 });

错误快照如下所示:

Snapshot for error is shown below:

推荐答案

最后我能够解决问题。在接收端,我们需要将字节转换为可读的字符串,而不是使用JSON解析器可读的解析。解决方案如下所示:

Finally i am able to solve the problem.On the receiving side, we need to convert byte in to readable string and than parse readable using JSON parser. Solution is shown below:

var mqtt=require('mqtt');
var client=mqtt.connect('mqtt://test.mosquitto.org:1883');  
client.subscribe('tempMeasurement');
client.on('message',function(topic,payload){
if(topic.toString()=="tempMeasurement"){
console.log("Message received");
var data =payload.toString('utf8',7);
var temp=JSON.parse(data);
console.log(temp.tempValue,temp.unitOfMeasurement);
//console.log(data);
}  
});

这篇关于node.js和java之间的mqtt通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆