diff --git a/modules/gateway/agent.go b/modules/gateway/agent.go index 4aadb1872..f4106296c 100644 --- a/modules/gateway/agent.go +++ b/modules/gateway/agent.go @@ -212,7 +212,7 @@ func (this *Agent) messageDistribution(msg *pb.UserMessage) (err error) { reply := &pb.RPCMessageReply{} log.Debugf("agent:%s uId:%s MessageDistribution msg:%s.%s", this.sessionId, this.uId, msg.MainType, msg.SubType) servicePath := comm.Service_Worker - if rule, ok := this.gateway.GetMsgDistribRule(msg.MainType, msg.SubType); ok { + if rule, ok := this.gateway.GetMsgDistribute(msg.MainType, msg.SubType); ok { servicePath = rule } else { if len(this.wId) > 0 { diff --git a/modules/gateway/agentmgr_comp.go b/modules/gateway/agentmgr_comp.go index c21b3002a..1bc209aaf 100644 --- a/modules/gateway/agentmgr_comp.go +++ b/modules/gateway/agentmgr_comp.go @@ -16,6 +16,7 @@ import ( /* 用户代理对象管理组件 */ + type AgentMgrComp struct { cbase.ModuleCompBase service base.IRPCXService diff --git a/modules/gateway/configure_comp.go b/modules/gateway/configure_comp.go index 0afe78a87..8b155e9bc 100644 --- a/modules/gateway/configure_comp.go +++ b/modules/gateway/configure_comp.go @@ -24,7 +24,7 @@ func (this *configureComp) Init(service core.IService, module core.IModule, comp } //获取消息分发规则读取配置表 -func (this *configureComp) GetMsgDistribRule(mtype, stype string) (rule string, ok bool) { +func (this *configureComp) GetMsgDistribute(mtype, stype string) (rule string, ok bool) { var ( err error v interface{} diff --git a/modules/gateway/core.go b/modules/gateway/core.go index 217c8a797..7159211ea 100644 --- a/modules/gateway/core.go +++ b/modules/gateway/core.go @@ -25,6 +25,6 @@ type ( Service() base.IRPCXService Connect(a IAgent) DisConnect(a IAgent) - GetMsgDistribRule(mtype, stype string) (rule string, ok bool) + GetMsgDistribute(mtype, stype string) (rule string, ok bool) } ) diff --git a/modules/gateway/module.go b/modules/gateway/module.go index ab704185e..722c80585 100644 --- a/modules/gateway/module.go +++ b/modules/gateway/module.go @@ -14,6 +14,7 @@ import ( 描述:提供客户端网关路由服务 管理用户socket对象 以及分发用户消息 开发:李伟 */ + func NewModule() core.IModule { m := new(Gateway) return m @@ -21,53 +22,59 @@ func NewModule() core.IModule { type Gateway struct { modules.ModuleBase - service base.IRPCXService - wsService *WSServiceComp //websocket 服务组件 提供websocket服务监听 - agentMgr *AgentMgrComp //用户代理对象管理组件 管理用户socket对象 + service base.IRPCXService // rpcx服务接口 主要client->server + wsService *WSServiceComp // websocket服务 监听websocket连接 + agentMgr *AgentMgrComp // 客户端websocket连接管理 configure *configureComp } -//模块名 +// GetType 获取模块服务类型 func (this *Gateway) GetType() core.M_Modules { return comm.ModuleGate } -//模块自定义参数 +// NewOptions 模块自定义参数 func (this *Gateway) NewOptions() (options core.IModuleOptions) { return new(Options) } -//提供服务对象获取接口 +// Service 获取rpcx服务接口 func (this *Gateway) Service() base.IRPCXService { return this.service } -//模块初始化函数 +// Init 模块初始化函数 func (this *Gateway) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(base.IRPCXService) return } -//模块启动函数 注册rpc服务接口提供用户相关的rpc接口服务 +// Start 模块启动函数 注册rpc服务接口提供用户相关的rpc接口服务 func (this *Gateway) Start() (err error) { - //注册用户绑定uid接口 登录成功后触发 - this.service.RegisterFunctionName(string(comm.Rpc_GatewayAgentBind), this.agentMgr.Bind) - //注册用户解绑uid接口 登出或则切换账号是触发 - this.service.RegisterFunctionName(string(comm.Rpc_GatewayAgentUnBind), this.agentMgr.UnBind) - //向用户发送消息接口 - this.service.RegisterFunctionName(string(comm.Rpc_GatewayAgentSendMsg), this.agentMgr.SendMsgToAgent) - //向多个用户对象发送消息接口 - this.service.RegisterFunctionName(string(comm.Rpc_GatewaySendBatchMsg), this.agentMgr.SendMsgToAgents) - //向所有用户发送消息接口 - this.service.RegisterFunctionName(string(comm.Rpc_GatewaySendRadioMsg), this.agentMgr.SendMsgToAllAgent) - //关闭用户socket连接接口 - this.service.RegisterFunctionName(string(comm.Rpc_GatewayAgentClose), this.agentMgr.CloseAgent) + _name2Func := map[string]any{ + // 注册用户绑定uid接口 登录成功后触发 + string(comm.Rpc_GatewayAgentBind): this.agentMgr.Bind, + // 注册用户解绑uid接口 登出或则切换账号是触发 + string(comm.Rpc_GatewayAgentUnBind): this.agentMgr.UnBind, + // 向用户发送消息接口 + string(comm.Rpc_GatewayAgentSendMsg): this.agentMgr.SendMsgToAgent, + // 向多个用户对象发送消息接口 + string(comm.Rpc_GatewaySendBatchMsg): this.agentMgr.SendMsgToAgents, + // 向所有用户发送消息接口 + string(comm.Rpc_GatewaySendRadioMsg): this.agentMgr.SendMsgToAllAgent, + // 关闭用户socket连接接口 + string(comm.Rpc_GatewayAgentClose): this.agentMgr.CloseAgent, + } + for name, fn := range _name2Func { + this.service.RegisterFunctionName(name, fn) + } err = this.ModuleBase.Start() return + } -//装备组件 +// OnInstallComp 装备组件 func (this *Gateway) OnInstallComp() { this.ModuleBase.OnInstallComp() this.agentMgr = this.RegisterComp(new(AgentMgrComp)).(*AgentMgrComp) @@ -75,19 +82,19 @@ func (this *Gateway) OnInstallComp() { this.configure = this.RegisterComp(new(configureComp)).(*configureComp) } -//有新的连接对象进入 +// Connect 有新的连接对象进入 func (this *Gateway) Connect(a IAgent) { log.Debugf("[Module.Gateway] have new connect:Ip[%s] SessionId:[%s]", a.IP(), a.SessionId()) this.agentMgr.Connect(a) } -//有用户断开连接 +// DisConnect 有用户断开连接 func (this *Gateway) DisConnect(a IAgent) { log.Debugf("[Module.Gateway] have disConnect:Ip[%s] SessionId:[%s] uid:[%s]", a.IP(), a.SessionId(), a.UserId()) this.agentMgr.DisConnect(a) } -//读取消息分发规则 -func (this *Gateway) GetMsgDistribRule(mtype, stype string) (rule string, ok bool) { - return this.configure.GetMsgDistribRule(mtype, stype) +// GetMsgDistribute 读取消息分发规则 +func (this *Gateway) GetMsgDistribute(mtype, stype string) (rule string, ok bool) { + return this.configure.GetMsgDistribute(mtype, stype) }