简体   繁体   English

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

[英]exchanging messages between server(node.js) and client(python/node.js) via protobuf

I'm studying protobuf protocol and got the problem when server(node js) + client(python) do not want to exchange msgs.我正在研究 protobuf 协议,当服务器(节点 js)+ 客户端(python)不想交换 msgs 时遇到了问题。 Between server(node js) + client(node js) no problem.在服务器(节点 js)+ 客户端(节点 js)之间没问题。 Both clients use the same server.两个客户端使用相同的服务器。 I found difference between python and node js clients, after serialization msg byte arrays are different and it can be a cause of issue.我发现 python 和节点 js 客户端之间的区别,在序列化 msg 字节 arrays 之后是不同的,这可能是问题的原因。 But my knowledge is not enough to fix the issue.但是我的知识不足以解决这个问题。 May be someone see the problem?可能有人看到问题了吗?

client js客户端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'}
});

client py客户端py

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

server receives: client js: 08 b9 60 client py: 0a 03 08 b9 60服务器接收:客户端 js:08 b9 60 客户端 py:0a 03 08 b9 60

Also i found this topic: Serialization problem using protobuf between python and nodejs If it is encoding issue how to encode properly?我还发现了这个主题: 在 python 和 nodejs 之间使用 protobuf 的序列化问题如果是编码问题,如何正确编码?

Problem detected, instead of:检测到问题,而不是:

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

should be:应该:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM