node.js和java之间的mqtt通信

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

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发布数据的代码如下:

 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)

 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); }); 

错误快照如下所示: Node.js控制台出错

MQTT消息有效负载只是字节数组,因此您需要了解消息在发送端和接收端的编码方式。 在这种情况下,由于您发布的代码只是传递Java对象,因此不清楚您要发送的是什么。

图中的错误消息表示该对象的Java序列化版本正在作为消息有效负载发送。 虽然这将包含您需要在JavaScript中重新组装的信息将非常困难。

假设TempStruct对象看起来像这样:

 public class TempStruct { int value = 0; String units = "C"; public void setValue(int val) { value = val; } public int getValue() { return value; } public void setUnits(String unit) { units = unit; } public String getUnits() { return units; } } 

然后你应该添加以下方法:

 public String toJSON() { String json = String.format("{'value': %d, 'units': '%s'}", value, units); return json; } 

然后编辑Java发布代码,如下所示:

 ... this.myPubSubMiddleware.publish("tempMeasurement", newValue.toJSON().getBytes("utf-8"), myDeviceInfo); ... 

并像这样更改您的JavaScript:

 ... client.on('message',function(topic,payload){ console.log("In messgage...."); var tempStruct = JSON.parse(payload.payloadString) console.log("tempValue: "+tempStruct.value); }); ... 

编辑:

将getBytes(“utf-8”)添加到Java代码中以确保我们只是将字符串字节放在消息中。

EDIT2:抱歉,将paho web客户端与npm mqtt模块混淆,JavaScript应该是:

 ... client.on('message',function(topic,payload){ console.log("In messgage...."); var tempStruct = JSON.parse(payload.toString()) console.log("tempValue: "+tempStruct.value); }); ... 

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

 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); } });