网关路由消息规则,还需要完善
This commit is contained in:
parent
4418621fb3
commit
a0ee2a3dd3
21
server/gate/server/processor.go
Normal file
21
server/gate/server/processor.go
Normal 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
|
||||||
|
}
|
@ -11,6 +11,7 @@ import (
|
|||||||
"game/server/gate/model"
|
"game/server/gate/model"
|
||||||
"github.com/fox/fox/etcd"
|
"github.com/fox/fox/etcd"
|
||||||
"github.com/fox/fox/log"
|
"github.com/fox/fox/log"
|
||||||
|
"github.com/fox/fox/processor"
|
||||||
"github.com/fox/fox/service"
|
"github.com/fox/fox/service"
|
||||||
"github.com/fox/fox/ws"
|
"github.com/fox/fox/ws"
|
||||||
"github.com/fox/fox/xrand"
|
"github.com/fox/fox/xrand"
|
||||||
@ -23,6 +24,7 @@ type GateService struct {
|
|||||||
*service.NatsService
|
*service.NatsService
|
||||||
etcdService *etcd.Registry[etcd.ServiceNode]
|
etcdService *etcd.Registry[etcd.ServiceNode]
|
||||||
wss *ws.WsServer
|
wss *ws.WsServer
|
||||||
|
processor *processor.Processor
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
@ -46,6 +48,7 @@ func Stop() {
|
|||||||
func newGateService(serviceId int) *GateService {
|
func newGateService(serviceId int) *GateService {
|
||||||
var err error
|
var err error
|
||||||
s := new(GateService)
|
s := new(GateService)
|
||||||
|
|
||||||
sName := fmt.Sprintf("%v-%d", serviceName.Gate, serviceId)
|
sName := fmt.Sprintf("%v-%d", serviceName.Gate, serviceId)
|
||||||
if s.NatsService, err = service.NewNatsService(serviceName.Gate, sName, s, config.Cfg.Nats.Address...); err != nil {
|
if s.NatsService, err = service.NewNatsService(serviceName.Gate, sName, s, config.Cfg.Nats.Address...); err != nil {
|
||||||
log.Fatal(err.Error())
|
log.Fatal(err.Error())
|
||||||
@ -73,6 +76,9 @@ func newGateService(serviceId int) *GateService {
|
|||||||
s.NatsService.OnStop()
|
s.NatsService.OnStop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.processor = processor.NewProcessor()
|
||||||
|
s.initProcessor()
|
||||||
s.OnInit()
|
s.OnInit()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@ -98,9 +104,30 @@ func (s *GateService) OnStop() {
|
|||||||
log.Debug("OnStop")
|
log.Debug("OnStop")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通过handler转发消息
|
// 处理其它服发送过来的消息。大部分是将消息转发给玩家
|
||||||
func (s *GateService) OnMessage(msg []byte) error {
|
func (s *GateService) OnMessage(data []byte) error {
|
||||||
log.Debug(s.Log("on message:%v", string(msg)))
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +170,7 @@ func (s *GateService) findTopic(userId int64, serviceTypeId pb.ServiceTypeId) st
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// 运行在conn的read协程里
|
// 运行在conn的read协程里。将消息转发给内部服务
|
||||||
func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) {
|
func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) {
|
||||||
msg := &pb.ClientMsg{}
|
msg := &pb.ClientMsg{}
|
||||||
if err := proto.Unmarshal(data, msg); err != nil {
|
if err := proto.Unmarshal(data, msg); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user