[英]Protobuf Java method parseFrom() hangs when receiving message from MQTT Server
I'm currently trying to publish and receive Protobuf messages via a Mosquitto MQQT Server.我目前正在尝试通过 Mosquitto MQQT 服务器发布和接收 Protobuf 消息。 I am succesfully publishing the proper to the Server.
我成功地向服务器发布了正确的内容。 However, when a client receives it, the method parseFrom() hangs and never returns.
但是,当客户端收到它时,方法 parseFrom() 挂起并且永远不会返回。 This is a very similar issue to this one , occuring when a Protobuf message is sent via a Socket that's never closed.
这是一个非常类似的问题这一个,当通过一个插口,从来没有关闭时发出的Protobuf消息存在的。
Publisher :出版商:
MqttClient adapterClient = new MqttClient(broker, clientID);
SpecsMessage.Specs protoNotifyMessage = SpecsMessage.Specs.newBuilder()
.setNodeType("basic")
.setAddress(serverSocket.getInetAddress().getHostName())
.setPort(serverSocket.getLocalPort())
.build();
MqttMessage notifyMessage = new MqttMessage(protoNotifyMessage.toString().getBytes());
adapterClient.publish("availableNodes", notifyMessage);
Subscriber :订阅者:
public class TestController implements MqttCallback {
public void messageArrived(String topic, MqttMessage message){
System.out.println("New node connected");
System.out.println("Payload: \n" + new String(message.getPayload()));
SpecsMessage.Specs protoMessage = SpecsMessage.Specs.parseFrom(message.getPayload());
}
}
I could not find a way to specify to the MQQT Server a correct way to send the message.我找不到向 MQQT 服务器指定发送消息的正确方式的方法。
I also tried using the writeDelimitedFrom() method.我也尝试使用 writeDelimitedFrom() 方法。
MqttClient adapterClient = new MqttClient(broker, clientID);
SpecsMessage.Specs protoNotifyMessage = SpecsMessage.Specs.newBuilder()
.setNodeType("basic")
.setAddress(serverSocket.getInetAddress().getHostName())
.setPort(serverSocket.getLocalPort())
.build();
ByteArrayOutputStream output = new ByteArrayOutputStream();
protoNotifyMessage.writeDelimitedTo(output);
MqttMessage notifyMessage = new MqttMessage(output.toByteArray());
adapterClient.publish("availableNodes", notifyMessage);
However, the message is not correctly converted in byte[], here's what it should look like :但是,消息在字节 [] 中没有正确转换,它应该是这样的:
nodeType: "basic"
address: "0.0.0.0"
port: 43101
And here's what I get :这就是我得到的:
basic0.0.0.0��
Is there a way to make this work, either by correcting the sending method or by solving the byte[] conversion problem?有没有办法让这个工作,要么通过更正发送方法,要么通过解决 byte[] 转换问题?
You're trying to use parseFrom
to parse the text-formatted proto.您正在尝试使用
parseFrom
来解析文本格式的原型。 parseFrom
is for parsing the wire format. parseFrom
用于解析有线格式。
Send the proto in the wire format instead - message.toByteArray()
.改为以有线格式发送 proto -
message.toByteArray()
。
(If you want to parse from the text format, use TextFormat
). (如果要从文本格式解析,请使用
TextFormat
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.