网关路由消息规则,还需要完善

This commit is contained in:
liuxiaobo 2025-05-27 00:55:17 +08:00
parent 4418621fb3
commit a0ee2a3dd3
2 changed files with 52 additions and 4 deletions

View File

@ -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
}

View File

@ -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 {