From a0ee2a3dd330f33fa5956d5d99ccef9a047498d8 Mon Sep 17 00:00:00 2001 From: liuxiaobo <1224730913@qq.com> Date: Tue, 27 May 2025 00:55:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E5=85=B3=E8=B7=AF=E7=94=B1=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=A7=84=E5=88=99=EF=BC=8C=E8=BF=98=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/gate/server/processor.go | 21 ++++++++++++++++++++ server/gate/server/service.go | 35 +++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 server/gate/server/processor.go diff --git a/server/gate/server/processor.go b/server/gate/server/processor.go new file mode 100644 index 0000000..702f65c --- /dev/null +++ b/server/gate/server/processor.go @@ -0,0 +1,21 @@ +package server + +import ( + "game/common/proto/pb" + "github.com/fox/fox/processor" + "github.com/fox/fox/ws" +) + +/* +gate只处理内部消息(ipb中的消息),避免拦截到外部消息,导致不转发给玩家 +*/ +func (s *GateService) initProcessor() { + s.processor.RegisterMessages(processor.RegisterMetas{ + pb.LoginMsgId_NtfUserOnline: {pb.NtfUserOnlineMsg{}, s.onNtfUserOnline}, + }) +} + +func (s *GateService) onNtfUserOnline(conn ws.IConn, msg *pb.NtfUserOnlineMsg) { + _ = conn + _ = msg +} diff --git a/server/gate/server/service.go b/server/gate/server/service.go index 1b75dbb..972c404 100644 --- a/server/gate/server/service.go +++ b/server/gate/server/service.go @@ -11,6 +11,7 @@ import ( "game/server/gate/model" "github.com/fox/fox/etcd" "github.com/fox/fox/log" + "github.com/fox/fox/processor" "github.com/fox/fox/service" "github.com/fox/fox/ws" "github.com/fox/fox/xrand" @@ -23,6 +24,7 @@ type GateService struct { *service.NatsService etcdService *etcd.Registry[etcd.ServiceNode] wss *ws.WsServer + processor *processor.Processor } func Init() { @@ -46,6 +48,7 @@ func Stop() { func newGateService(serviceId int) *GateService { var err error s := new(GateService) + sName := fmt.Sprintf("%v-%d", serviceName.Gate, serviceId) if s.NatsService, err = service.NewNatsService(serviceName.Gate, sName, s, config.Cfg.Nats.Address...); err != nil { log.Fatal(err.Error()) @@ -73,6 +76,9 @@ func newGateService(serviceId int) *GateService { s.NatsService.OnStop() return nil } + + s.processor = processor.NewProcessor() + s.initProcessor() s.OnInit() return s } @@ -98,9 +104,30 @@ func (s *GateService) OnStop() { log.Debug("OnStop") } -// 通过handler转发消息 -func (s *GateService) OnMessage(msg []byte) error { - log.Debug(s.Log("on message:%v", string(msg))) +// 处理其它服发送过来的消息。大部分是将消息转发给玩家 +func (s *GateService) OnMessage(data []byte) error { + var iMsg = &ipb.InternalMsg{} + var err error + if err = proto.Unmarshal(data, iMsg); err != nil { + log.Error(err.Error()) + return err + } + var conn ws.IConn + if conn, _ = s.wss.FindConnByUserId(iMsg.UserId); conn == nil { + return err + } + if req, err := s.processor.Unmarshal(iMsg.MsgId, iMsg.Msg); err == nil { + err = s.processor.Dispatch(iMsg.MsgId, conn, req) + } else { + var cMsg = &pb.ClientMsg{ + ServiceId: 0, + MsgId: iMsg.MsgId, + Data: iMsg.Msg, + } + cData, _ := proto.Marshal(cMsg) + _ = conn.SendMsg(cData) + } + //log.Debug(s.Log("on message:%v", string(msg))) return nil } @@ -143,7 +170,7 @@ func (s *GateService) findTopic(userId int64, serviceTypeId pb.ServiceTypeId) st return "" } -// 运行在conn的read协程里 +// 运行在conn的read协程里。将消息转发给内部服务 func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) { msg := &pb.ClientMsg{} if err := proto.Unmarshal(data, msg); err != nil {