@关于toLua封装和解析ProtoBuffer
封装ProtoBuffer发送给服务器
local serverId = 127.0.0.1;
function Net.WrapAndSendMessageToServer(msgId,msgBody)
local msg = msgBody:SerializeToString();
local buffer = ByteBuffer.New();
buffer:WriteProtoBuffer(msg);
netWorkManager:SendMessage(msgId,serverId,buffer);
end
function Net.LoginRequest(account,pwd,nickName,sex)
local register = login_pb.C2L_Register();
register.Appver = AppInfo.version;
register.Account = account;
register.pwd = pwd;
…
register.sex = sex;
this.WrapAndSendMessageToServer(packetId_pb.C2L_Req_Register,register);
end
–解析
function Net.RegisterResponse(buffer)
local data = buffer:ReadProtoBuffe()r;
local msg = login_pb.L2C_RespRegister();
msg:ParseFormString(data);
Event.Brocast(EventNames.RegisterResponse,msg);
end