IMessage body; UserLoginRequest req = new UserLoginRequest(); req.AuthToken = "8"; Request sy = new Request(); sy.Token = "hello"; sy.Body = Any.Pack(req); byte[] bs = sy.ToByteArray(); string hexString = BitConverter.ToString(bs); Debug.Log($"len:{bs.Length}"); Debug.Log(hexString);
import { readFileSync } from "fs" import * as pb from "protobufjs" let popContent = readFileSync("pop.json") let popRoot = pb.Root.fromJSON(JSON.parse(popContent.toString())) let UserRequest:pb.Type = popRoot.lookupType("ProtoSync.UserLoginRequest") let UserMsg = UserRequest.create({ authToken: "8"}) let Userbuffer = UserRequest.encode(UserMsg).finish() console.log(Userbuffer) let pubContent:Buffer = readFileSync("proto.json") let pubRoot:pb.Root = pb.Root.fromJSON(JSON.parse(pubContent.toString())) let Request:pb.Type = pubRoot.lookupType("ProtoSync.Request") // 创建包含 Userbuffer 的 Any 类型结构 // 创建 ByteBuffer 对象并写入 Uint8Array 数据 const byteBuffer = pb.util.newBuffer(Userbuffer.length); byteBuffer.set(Userbuffer); const anyValue = { type_url: "type.googleapis.com/ProtoSync.UserLoginRequest", value: Userbuffer,//c#里这里有个byteString类型,ts中没有,蛋疼 }; //let reqMsg:pb.Message<{}> = Request.create({ token: "hello", body: UserMsg }) let reqMsg:pb.Message<{}> = Request.create({ token: "hello", body: anyValue }) //let reqMsg:pb.Message<{}> = Request.create({ token: "hello"}) let buffer:Uint8Array = Request.encode(reqMsg).finish() console.log(`len:${buffer.length}`) console.log(buffer)
标签:typescript,create,Request,pb,let,Userbuffer,Any,Pack From: https://www.cnblogs.com/nafio/p/17492970.html