181 lines
5.1 KiB
Go
181 lines
5.1 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/base"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"time"
|
|
|
|
"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.ModuleChat
|
|
}
|
|
|
|
//模块初始化接口 注册用户创建角色事件
|
|
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, err)
|
|
}
|
|
|
|
//对外接口----------------------------------------------------------------------------------------------------------
|
|
//向世界频道发送聊天消息
|
|
func (this *Chat) SendWorldChat(msg *pb.DBChat) (code pb.ErrorCode) {
|
|
code = this.modelChat.sendWorldChat(msg)
|
|
return
|
|
}
|
|
|
|
//向个人发送聊天消息
|
|
func (this *Chat) SendUserChat(msg *pb.DBChat) (code pb.ErrorCode) {
|
|
var (
|
|
err error
|
|
)
|
|
if session, ok := this.GetUserSession(msg.Ruid); ok {
|
|
session.SendMsg(string(this.GetType()), "message", &pb.ChatMessagePush{Chat: msg})
|
|
if err = session.Push(); err != nil {
|
|
this.Errorf("err:%v", err)
|
|
code = pb.ErrorCode_SystemError
|
|
}
|
|
return
|
|
} else {
|
|
if err = this.modelChat.SaveUserMsg(msg); err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//广播系统消息
|
|
func (this *Chat) SendSysChatToWorld(ctype comm.ChatSystemType, value int32, agrs ...interface{}) (code pb.ErrorCode) {
|
|
if st, ok := this.configure.GetCheckChatSystem(int32(ctype), value); ok {
|
|
msg := &pb.DBChat{
|
|
Channel: pb.ChatChannel_System,
|
|
Stag: this.service.GetTag(),
|
|
Ctime: time.Now().Unix(),
|
|
Content: fmt.Sprintf(st.Text, agrs...),
|
|
}
|
|
code = this.modelChat.sendSystemChat(msg)
|
|
}
|
|
return
|
|
}
|
|
|
|
//广播系统消息
|
|
func (this *Chat) SendSysChatToUser(session comm.IUserSession, ctype comm.ChatSystemType, value int32, agrs ...interface{}) (code pb.ErrorCode) {
|
|
if st, ok := this.configure.GetCheckChatSystem(int32(ctype), value); ok {
|
|
msg := &pb.DBChat{
|
|
Channel: pb.ChatChannel_System,
|
|
Ctime: time.Now().Unix(),
|
|
Content: fmt.Sprintf(st.Text, agrs...),
|
|
}
|
|
session.SendMsg(string(this.GetType()), "message", &pb.ChatMessagePush{Chat: msg})
|
|
}
|
|
return
|
|
}
|
|
|
|
//Push--------------------------------------------------------------------------------------------------------------
|
|
//推送消息到世界
|
|
func (this *Chat) PushWorld(msg *pb.DBChat) (err error) {
|
|
data, _ := anypb.New(&pb.ChatMessagePush{Chat: msg})
|
|
if err = this.service.AcrossClusterBroadcast(context.Background(), msg.Stag, comm.Service_Gateway, string(comm.Rpc_GatewaySendRadioMsg), pb.UserMessage{
|
|
MainType: string(this.GetType()),
|
|
SubType: "message",
|
|
Data: data,
|
|
}, nil); err != nil {
|
|
this.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
//推送消息到工会
|
|
func (this *Chat) PushUnion(unionId string, msg *pb.DBChat) (err error) {
|
|
return
|
|
}
|
|
|
|
//推送消息到用户
|
|
func (this *Chat) PushUser(msg *pb.DBChat) (err error) {
|
|
if session, ok := this.GetUserSession(msg.Ruid); ok {
|
|
session.SendMsg(string(this.GetType()), "message", &pb.ChatMessagePush{Chat: msg})
|
|
if err = session.Push(); err != nil {
|
|
this.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
} else {
|
|
err = this.modelChat.SaveUserMsg(msg)
|
|
}
|
|
return
|
|
}
|
|
|
|
//推送消息到指定用户群
|
|
func (this *Chat) PushToUsers(channel int32, msg *pb.DBChat) (err error) {
|
|
var (
|
|
users []*pb.CacheUser
|
|
)
|
|
if users, err = this.modelChat.GetCrossChannelMember(channel); err == nil {
|
|
if err = this.SendMsgToUsers(string(this.GetType()), "message", &pb.ChatMessagePush{Chat: msg}, users...); err != nil {
|
|
this.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//全集群推送
|
|
func (this *Chat) PushAllWorld(msg *pb.DBChat) (err error) {
|
|
data, _ := anypb.New(&pb.ChatMessagePush{Chat: msg})
|
|
if err = this.service.ClusterBroadcast(context.Background(), comm.Service_Gateway, string(comm.Rpc_GatewaySendRadioMsg), pb.UserMessage{
|
|
MainType: string(this.GetType()),
|
|
SubType: "message",
|
|
Data: data,
|
|
}, nil); err != nil {
|
|
this.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
}
|