func (user *ChatUser) packetHandler(packet []byte) { var ( err error ) //数据解码 var chatPackat *chat.ChatPacket = user.decodePacket(packet) cmd := chatPackat.GetCmd() switch cmd { case cmd_login_req: loginReq := &chat.LoginReq{} err = proto.Unmarshal(chatPackat.GetData(), loginReq) util.CheckError(err, "proto.Unmarshal cmd_login_req: ") user.loginReq_Handle(loginReq) case cmd_sendmsg_req: sendMsgReq := &chat.SendmsgReq{} err = proto.Unmarshal(chatPackat.GetData(), sendMsgReq) util.CheckError(err, "proto.Unmarshal cmd_sendmsg_req: ") user.sendMsgReq_Handle(sendMsgReq) case cmd_ping: pingReq := &chat.Ping{} err = proto.Unmarshal(chatPackat.GetData(), pingReq) util.CheckError(err, "proto.Unmarshal cmd_ping: ") default: fmt.Println("error cmd:", cmd) } }
func (cp ChatPacket) decodePacket(chatPacketData []byte) *chat.ChatPacket { var chatPackat *chat.ChatPacket = &chat.ChatPacket{} err := proto.Unmarshal(chatPacketData, chatPackat) util.CheckError(err, "proto.Unmarshal: ") return chatPackat }