go_dreamfactory/modules/chat/module.go
2022-07-20 14:01:59 +08:00

131 lines
3.4 KiB
Go

package chat
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"google.golang.org/protobuf/types/known/anypb"
)
/*
模块名:论坛
描述:处理跨服社交论坛相关业务
开发:李伟
*/
func NewModule() core.IModule {
m := new(Chat)
return m
}
type Chat struct {
modules.ModuleBase
service base.IRPCXService //rpc服务对象 通过这个对象可以发布服务和调用其他服务的接口
api_comp *apiComp
configure *configureComp
modelChat *modelChatComp
}
//模块名
func (this *Chat) GetType() core.M_Modules {
return comm.ModuleEquipment
}
//模块初始化接口 注册用户创建角色事件
func (this *Chat) 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
}
func (this *Chat) Start() (err error) {
err = this.ModuleBase.Start()
event.RegisterGO(comm.EventUserOffline, this.EventUserOffline)
return
}
//装备组件
func (this *Chat) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelChat = this.RegisterComp(new(modelChatComp)).(*modelChatComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
//Event------------------------------------------------------------------------------------------------------------
func (this *Chat) EventUserOffline(session comm.IUserSession) {
err := this.modelChat.RemoveCrossChannelMember(session)
this.Debugf("EventUserOffline:%s err:%v", session.GetUserId(), err)
}
//Push--------------------------------------------------------------------------------------------------------------
//推送消息到世界
func (this *Chat) PushWorld(msg *pb.DBChat) {
var (
err error
reply *pb.RPCMessageReply
)
reply = &pb.RPCMessageReply{}
data, _ := anypb.New(msg)
if err = this.service.RpcBroadcast(context.Background(), comm.Service_Gateway, string(comm.Rpc_GatewaySendRadioMsg), pb.UserMessage{
MainType: string(this.GetType()),
SubType: "push",
Data: data,
}, reply); err != nil {
this.Errorf("err:%v", err)
}
}
//推送消息到工会
func (this *Chat) PushUnion(msg *pb.DBChat) {
}
//推送消息到用户
func (this *Chat) PushUser(msg *pb.DBChat) {
if session, ok := this.GetUserSession(msg.Ruid); ok {
session.SendMsg(string(this.GetType()), "push", msg)
if err := session.Push(); err != nil {
this.Errorf("err:%v", err)
}
return
} else {
this.modelChat.SaveUserMsg(msg)
}
}
//全集群推送
func (this *Chat) PushToUsers(group, channel int32, msg *pb.DBChat) {
var (
err error
users []*pb.CacheUser
)
if users, err = this.modelChat.GetCrossChannelMember(group, channel); err == nil {
if err = this.SendMsgToUsers(string(this.GetType()), "push", msg, users...); err != nil {
this.Errorf("err:%v", err)
return
}
}
}
//全集群推送
func (this *Chat) PushAllWorld(msg *pb.DBChat) {
var (
err error
reply *pb.RPCMessageReply
)
reply = &pb.RPCMessageReply{}
data, _ := anypb.New(msg)
if err = this.service.ClusterBroadcast(context.Background(), comm.Service_Gateway, string(comm.Rpc_GatewaySendRadioMsg), pb.UserMessage{
MainType: string(this.GetType()),
SubType: "push",
Data: data,
}, reply); err != nil {
this.Errorf("err:%v", err)
}
}