go_dreamfactory/modules/gateway/module.go
2022-06-29 14:40:59 +08:00

94 lines
3.0 KiB
Go

package gateway
import (
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
)
/*
模块名:gateway
描述:提供客户端网关路由服务 管理用户socket对象 以及分发用户消息
开发:李伟
*/
func NewModule() core.IModule {
m := new(Gateway)
return m
}
type Gateway struct {
modules.ModuleBase
service base.IRPCXService
wsService *WSServiceComp //websocket 服务组件 提供websocket服务监听
agentMgr *AgentMgrComp //用户代理对象管理组件 管理用户socket对象
configure *configureComp
}
//模块名
func (this *Gateway) GetType() core.M_Modules {
return comm.ModuleGate
}
//模块自定义参数
func (this *Gateway) NewOptions() (options core.IModuleOptions) {
return new(Options)
}
//提供服务对象获取接口
func (this *Gateway) Service() base.IRPCXService {
return this.service
}
//模块初始化函数
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接口服务
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)
err = this.ModuleBase.Start()
return
}
//装备组件
func (this *Gateway) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.agentMgr = this.RegisterComp(new(AgentMgrComp)).(*AgentMgrComp)
this.wsService = this.RegisterComp(new(WSServiceComp)).(*WSServiceComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
//有新的连接对象进入
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)
}
//有用户断开连接
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)
}