101 lines
3.1 KiB
Go
101 lines
3.1 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 // 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
|
|
}
|
|
|
|
// Start 模块启动函数 注册rpc服务接口提供用户相关的rpc接口服务
|
|
func (this *Gateway) Start() (err error) {
|
|
_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)
|
|
this.wsService = this.RegisterComp(new(WSServiceComp)).(*WSServiceComp)
|
|
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)
|
|
}
|
|
|
|
// GetMsgDistribute 读取消息分发规则
|
|
func (this *Gateway) GetMsgDistribute(mtype, stype string) (rule string, ok bool) {
|
|
return this.configure.GetMsgDistribute(mtype, stype)
|
|
}
|