From 1e02964ec5037f594803c81891eeefb5460138b3 Mon Sep 17 00:00:00 2001 From: liuxiaobo <1224730913@qq.com> Date: Thu, 29 May 2025 20:47:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0client=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/client/server/login.go | 27 +++++++++++++++++++++++++++ test/client/server/processor.go | 2 +- test/client/server/service.go | 15 ++++++++++----- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 test/client/server/login.go diff --git a/test/client/server/login.go b/test/client/server/login.go new file mode 100644 index 0000000..de49667 --- /dev/null +++ b/test/client/server/login.go @@ -0,0 +1,27 @@ +package server + +import ( + "game/common/proto/pb" + "github.com/fox/fox/log" +) + +func (s *ClientService) login() { + s.SendMsg(pb.ServiceTypeId_STI_Login, int32(pb.MsgId_C2SUserLoginId), &pb.C2SUserLogin{ + Username: s.username, + Password: s.password, + Ip: "", + DeviceId: "", + Version: "", + }) +} + +// 收到登陆成功消息,判断是否顶号 +func (s *ClientService) onLogin(cMsg *pb.ClientMsg, msg *pb.S2CUserLogin) { + if msg.Code != pb.ErrCode_OK { + log.ErrorF("login error: %v", msg.Code) + return + } + _ = cMsg + s.userId = msg.UserId + log.DebugF("user:%v id:%v login success", s.username, msg.UserId) +} diff --git a/test/client/server/processor.go b/test/client/server/processor.go index b208673..27db690 100644 --- a/test/client/server/processor.go +++ b/test/client/server/processor.go @@ -7,7 +7,7 @@ import ( func (s *ClientService) initProcessor() { s.processor.RegisterMessages(processor.RegisterMetas{ - pb.MsgId_C2SChatId: {pb.C2SChat{}, s.onChat}, + pb.MsgId_S2CUserLoginId: {pb.S2CUserLogin{}, s.onLogin}, }) } diff --git a/test/client/server/service.go b/test/client/server/service.go index 033899b..58b82fc 100644 --- a/test/client/server/service.go +++ b/test/client/server/service.go @@ -13,9 +13,12 @@ import ( var Clients []*ClientService type ClientService struct { - client *ws.Client - processor *processor.Processor - userId int64 + client *ws.Client + processor *processor.Processor + userId int64 + username string + password string + lastServiceName string // 最近使用的服务的节点 } func Init() { @@ -37,6 +40,8 @@ func newClientService(serviceId int) *ClientService { var err error s := new(ClientService) + s.username = fmt.Sprintf("test%04d", serviceId) + s.password = "123456" size := len(config.Cfg.Special.Address) addr := config.Cfg.Special.Address[serviceId%size] if s.client, err = ws.NewClient(fmt.Sprintf("ws://%v", addr), s); err != nil { @@ -66,7 +71,7 @@ func (s *ClientService) OnMessage(data []byte) error { return err } if req, err := s.processor.Unmarshal(cMsg.MsgId, cMsg.Data); err == nil { - err = s.processor.Dispatch(cMsg.MsgId, cMsg.UserId, cMsg, req) + err = s.processor.Dispatch(cMsg.MsgId, cMsg, req) } //log.Debug(s.Log("on message:%v", string(cMsg))) return nil @@ -76,7 +81,7 @@ func (s *ClientService) OnMessage(data []byte) error { func (s *ClientService) SendData(serviceTypeId pb.ServiceTypeId, msgId int32, data []byte) { msg := &pb.ClientMsg{ ServiceTid: serviceTypeId, - ServiceName: "", + ServiceName: s.lastServiceName, UserId: s.userId, MsgId: msgId, Data: data,