protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
330 stars 66 forks source link

【BUG】proto转js之后,反序列化方法没定义 #169

Closed DokiDoki1103 closed 1 year ago

DokiDoki1103 commented 1 year ago
proto.LoginResponse.prototype.serializeBinary = function() {
  var writer = new jspb.BinaryWriter();
  proto.LoginResponse.serializeBinaryToWriter(this, writer);
  return writer.getResultBuffer();
};

proto.LoginRequest.deserializeBinary = function(bytes) {
  var reader = new jspb.BinaryReader(bytes);
  var msg = new proto.LoginRequest;
  return proto.LoginRequest.deserializeBinaryFromReader(msg, reader);
};

由于反序列化的方法中没有prototype故无法通过官网那样new出来的对象调用deserializeBinary方法。

只能 import messages from "../proto/sso_pb" 后 messages.LoginResponse.deserializeBinary(res.data)

而不是像官网一样new

DokiDoki1103 commented 1 year ago

服务器返回了arraybuffer数据,怎么使用protojs文件解析呢?

dibenede commented 1 year ago

You are correct that deserializeBinary is a static method. Can you convert the ArrayBuffer to a Uint8Array?

DokiDoki1103 commented 1 year ago

我认为 deserializeBinary 方法应该定义为类的方法 如同这样 proto.LoginResponse.prototype.deserializeBinary 然后const msg = new Message()然后调用 meg.deserializeBinary() 直接反序列化为msg对象,直接调用getXx取值

dibenede commented 1 year ago

We believe deserializeBinary is naturally a static method. The semantics are oriented around constructing a message from a data blob rather than populating an existing message. We have no plans to change this.