通过protobuf在服务器(node.js)和客户机(python/node.js)之间交换消息



我正在研究protobuf协议,当服务器(node-js(+客户端(python(不想交换消息时遇到了问题。在服务器(node-js(+客户端(node.js(之间没有问题。两个客户端使用相同的服务器。我发现python和node-js客户端之间的差异,在序列化后,msg字节数组不同,这可能是问题的原因。但我的知识不足以解决这个问题。可能有人看到问题了吗?

客户端js

const instance = new Product.Id().setValue(12345);
let message = instance.serializeBinary();
console.log(message); //Uint8Array(3) [ 8, 185, 96 ]
let response = await fetch('http://localhost:3008/api/getById', {
method: 'POST',
body: message,
headers: {'Content-Type': 'application/protobuf'}
});

客户端py

instance.id.value = 12345
byte = instance.SerializeToString()
print(byte) #b'nx03x08xb9`'
response = requests.post(url = "http://localhost:3008/api/getById", headers={'Content-Type': 'application/protobuf'}, data=byte)

服务器接收:客户端js:08 b9 60客户电话:0a 03 08 b9 60

我还发现了这个主题:python和nodejs之间使用protobuf的序列化问题如果是编码问题,如何正确编码?

检测到问题,而不是:

instance.id.value = 12345
byte = instance.SerializeToString()

应该是:

instance.id.value = 12345
byte = instance.id.SerializeToString()

最新更新