上传聊天模块跨服成员聊天维护
This commit is contained in:
parent
3eef4844c6
commit
0031d7bfe8
@ -80,6 +80,6 @@ type IRPCXService interface {
|
||||
RpcBroadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error)
|
||||
RpcGo(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (call *client.Call, err error)
|
||||
AcrossClusterRpcCall(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error)
|
||||
AcrossClusterRpcGo(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (call *client.Call, err error)
|
||||
AcrossClusterRpcGo(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (call *client.Call, err error)
|
||||
ClusterBroadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error)
|
||||
}
|
||||
|
@ -15,6 +15,17 @@ func (this *apiComp) CrossChannelCheck(session comm.IUserSession, req *pb.ChatCr
|
||||
|
||||
///获取未读消息
|
||||
func (this *apiComp) CrossChannel(session comm.IUserSession, req *pb.ChatCrossChannelReq) (code pb.ErrorCode, data proto.Message) {
|
||||
this.module.modelChat.AddCrossChannelMember(session)
|
||||
var (
|
||||
channel int32
|
||||
err error
|
||||
)
|
||||
if channel, err = this.module.modelChat.AddCrossChannelMember(session); err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
this.module.modelChat.ChanageUserExpand(session.GetUserId(), map[string]interface{}{
|
||||
"ChatChannel": channel,
|
||||
})
|
||||
session.SendMsg(string(this.module.GetType()), "getlist", &pb.ChatCrossChannelResp{ChannelId: channel})
|
||||
return
|
||||
}
|
||||
|
@ -18,14 +18,20 @@ func (this *apiComp) SpanSendCheck(session comm.IUserSession, req *pb.ChatSpanSe
|
||||
///跨越服务消息请求
|
||||
func (this *apiComp) SpanSend(session comm.IUserSession, req *pb.ChatSpanSendReq) (code pb.ErrorCode, data proto.Message) {
|
||||
var (
|
||||
err error
|
||||
msg *pb.DBChat
|
||||
user *pb.DBUser
|
||||
err error
|
||||
msg *pb.DBChat
|
||||
user *pb.DBUser
|
||||
userexpand *pb.DBUserExpand
|
||||
)
|
||||
if userexpand, err = this.module.modelChat.GetUserExpand(session.GetUserId()); err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
msg = &pb.DBChat{
|
||||
Id: primitive.NewObjectID().Hex(),
|
||||
Channel: req.Channel,
|
||||
Suid: session.GetUserId(),
|
||||
AreaId: userexpand.ChatChannel,
|
||||
Headid: user.Avatar,
|
||||
Content: req.Content,
|
||||
Ctime: time.Now().Unix(),
|
||||
@ -37,10 +43,7 @@ func (this *apiComp) SpanSend(session comm.IUserSession, req *pb.ChatSpanSendReq
|
||||
session.SendMsg(string(this.module.GetType()), "send", &pb.ChatSendResp{})
|
||||
switch msg.Channel {
|
||||
case pb.ChatChannel_CrossServer:
|
||||
this.module.PushAllWorld(msg)
|
||||
break
|
||||
case pb.ChatChannel_System:
|
||||
this.module.PushAllWorld(msg)
|
||||
this.module.PushToUsers(userexpand.ChatChannel, msg)
|
||||
break
|
||||
}
|
||||
return
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/codec"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
|
||||
@ -63,7 +62,7 @@ func (this *modelChatComp) QueryUserMsg(uid string) (result []*pb.DBChat, err er
|
||||
}
|
||||
|
||||
//添加跨服频道成员
|
||||
func (this *modelChatComp) AddCrossChannelMember(session comm.IUserSession) (channel int, err error) {
|
||||
func (this *modelChatComp) AddCrossChannelMember(session comm.IUserSession) (channel int32, err error) {
|
||||
udata := &pb.CacheUser{
|
||||
Uid: session.GetUserId(),
|
||||
SessionId: session.GetSessionId(),
|
||||
@ -81,7 +80,7 @@ func (this *modelChatComp) AddCrossChannelMember(session comm.IUserSession) (cha
|
||||
}
|
||||
if count < 3000 {
|
||||
if err = this.Redis.HMSet(key, map[string]interface{}{session.GetUserId(): udata}); err != nil {
|
||||
log.Errorf("err%v", err)
|
||||
this.module.Errorf("err:%v", err)
|
||||
return
|
||||
}
|
||||
break
|
||||
@ -92,6 +91,34 @@ func (this *modelChatComp) AddCrossChannelMember(session comm.IUserSession) (cha
|
||||
return
|
||||
}
|
||||
|
||||
//读取跨服聊天频道下成员
|
||||
func (this *modelChatComp) GetCrossChannelMember(channel int32) (result []*pb.CacheUser, err error) {
|
||||
key := fmt.Sprintf("%s:%d-member", crosschatkey, channel)
|
||||
result = make([]*pb.CacheUser, 0)
|
||||
if err = this.Redis.HGetAll(key, &result); err != nil {
|
||||
this.module.Errorf("err:%v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//移除频道成员
|
||||
func (this *modelChatComp) RemoveCrossChannelMember(uid string) (err error) {
|
||||
var (
|
||||
result *pb.DBUserExpand
|
||||
)
|
||||
if result, err = this.GetUserExpand(uid); err != nil {
|
||||
this.module.Errorf("err:%v", err)
|
||||
return
|
||||
}
|
||||
key := fmt.Sprintf("%s:%d-member", crosschatkey, result.ChatChannel)
|
||||
if err = this.Redis.HDel(key, uid); err != nil {
|
||||
this.module.Errorf("err:%v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//添加聊天消息到数据库中
|
||||
func (this *modelChatComp) AddChatMsg(msg *pb.DBChat) (err error) {
|
||||
switch msg.Channel {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/base"
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/event"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
|
||||
@ -41,6 +42,12 @@ func (this *Chat) Init(service core.IService, module core.IModule, options core.
|
||||
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()
|
||||
@ -49,6 +56,12 @@ func (this *Chat) OnInstallComp() {
|
||||
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
|
||||
}
|
||||
|
||||
//Event------------------------------------------------------------------------------------------------------------
|
||||
func (this *Chat) EventUserOffline(uid string) {
|
||||
err := this.modelChat.RemoveCrossChannelMember(uid)
|
||||
this.Debugf("EventUserOffline:%s err:%v", uid, err)
|
||||
}
|
||||
|
||||
//Push--------------------------------------------------------------------------------------------------------------
|
||||
//推送消息到世界
|
||||
func (this *Chat) PushWorld(msg *pb.DBChat) {
|
||||
@ -85,6 +98,20 @@ func (this *Chat) PushUser(msg *pb.DBChat) {
|
||||
}
|
||||
}
|
||||
|
||||
//全集群推送
|
||||
func (this *Chat) PushToUsers(channel int32, msg *pb.DBChat) {
|
||||
var (
|
||||
err error
|
||||
users []*pb.CacheUser
|
||||
)
|
||||
if users, err = this.modelChat.GetCrossChannelMember(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 (
|
||||
|
@ -19,12 +19,14 @@ import (
|
||||
|
||||
type AgentMgrComp struct {
|
||||
cbase.ModuleCompBase
|
||||
options *Options
|
||||
service base.IRPCXService
|
||||
agents *sync.Map
|
||||
}
|
||||
|
||||
func (this *AgentMgrComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
err = this.ModuleCompBase.Init(service, module, comp, options)
|
||||
this.options = options.(*Options)
|
||||
this.service = service.(base.IRPCXService)
|
||||
this.agents = new(sync.Map)
|
||||
return
|
||||
@ -46,6 +48,13 @@ func (this *AgentMgrComp) DisConnect(a IAgent) {
|
||||
}, reply); err != nil {
|
||||
log.Errorf("uId:%s Rpc_NoticeUserClose err:%v", a.UserId(), err)
|
||||
}
|
||||
//推送跨服集群处理
|
||||
if _, err := this.service.AcrossClusterRpcGo(context.Background(), this.options.SpanServiceTag, comm.Service_Worker, string(comm.Rpc_NoticeUserClose), &pb.NoticeUserCloseReq{
|
||||
UserSessionId: a.SessionId(),
|
||||
UserId: a.UserId(),
|
||||
}, reply); err != nil {
|
||||
log.Errorf("uId:%s Rpc_NoticeUserClose err:%v", a.UserId(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,8 +12,9 @@ import (
|
||||
type (
|
||||
Options struct {
|
||||
modules.Options
|
||||
GinDebug bool //web引擎日志开关
|
||||
ListenPort int //websocket 监听端口
|
||||
GinDebug bool //web引擎日志开关
|
||||
ListenPort int //websocket 监听端口
|
||||
SpanServiceTag string //跨服集群
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -111,27 +111,33 @@ func (this *ModuleBase) SendMsgToUser(mainType, subType string, msg proto.Messag
|
||||
//向多个用户发送消息
|
||||
func (this *ModuleBase) SendMsgToUsers(mainType, subType string, msg proto.Message, user ...*pb.CacheUser) (err error) {
|
||||
var (
|
||||
gateways map[string][]string = make(map[string][]string)
|
||||
gateways map[string]map[string][]string = make(map[string]map[string][]string)
|
||||
cluster map[string][]string
|
||||
gateway []string
|
||||
ok bool
|
||||
)
|
||||
for _, v := range user {
|
||||
if gateway, ok = gateways[v.GatewayServiceId]; !ok {
|
||||
if cluster, ok = gateways[v.ServiceTag]; !ok {
|
||||
gateways[v.ServiceTag] = make(map[string][]string)
|
||||
}
|
||||
if gateway, ok = cluster[v.GatewayServiceId]; !ok {
|
||||
gateway = make([]string, 0)
|
||||
gateways[v.GatewayServiceId] = gateway
|
||||
cluster[v.GatewayServiceId] = gateway
|
||||
}
|
||||
gateway = append(gateway, v.SessionId)
|
||||
}
|
||||
reply := &pb.RPCMessageReply{}
|
||||
data, _ := anypb.New(msg)
|
||||
for k, v := range gateways {
|
||||
if _, err = this.service.RpcGo(context.Background(), fmt.Sprintf("%s/%s", comm.Service_Gateway, k), string(comm.Rpc_GatewayAgentSendMsg), &pb.BatchMessageReq{
|
||||
UserSessionIds: v,
|
||||
MainType: mainType,
|
||||
SubType: subType,
|
||||
Data: data,
|
||||
}, reply); err != nil {
|
||||
log.Errorf("SendMsgToUsers:%s->%s.%s err:%v", k, mainType, subType, err)
|
||||
for k1, v1 := range v {
|
||||
if _, err = this.service.AcrossClusterRpcGo(context.Background(), k1, fmt.Sprintf("%s/%s", comm.Service_Gateway, k), string(comm.Rpc_GatewayAgentSendMsg), &pb.BatchMessageReq{
|
||||
UserSessionIds: v1,
|
||||
MainType: mainType,
|
||||
SubType: subType,
|
||||
Data: data,
|
||||
}, reply); err != nil {
|
||||
log.Errorf("SendMsgToUsers:%s.%s->%s.%s err:%v", k1, k, mainType, subType, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
|
@ -7,4 +7,5 @@ message DBUserExpand {
|
||||
string id = 1; //主键id
|
||||
string uid = 2; //用户id
|
||||
int64 lastreadnotiftime = 3; //最后阅读公告时间
|
||||
}
|
||||
int32 ChatChannel = 4; //跨服聊天频道
|
||||
}
|
@ -29,6 +29,7 @@ type DBUserExpand struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id"` //主键id
|
||||
Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid"` //用户id
|
||||
Lastreadnotiftime int64 `protobuf:"varint,3,opt,name=lastreadnotiftime,proto3" json:"lastreadnotiftime"` //最后阅读公告时间
|
||||
ChatChannel int32 `protobuf:"varint,4,opt,name=ChatChannel,proto3" json:"ChatChannel"` //跨服聊天频道
|
||||
}
|
||||
|
||||
func (x *DBUserExpand) Reset() {
|
||||
@ -84,18 +85,27 @@ func (x *DBUserExpand) GetLastreadnotiftime() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *DBUserExpand) GetChatChannel() int32 {
|
||||
if x != nil {
|
||||
return x.ChatChannel
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var File_userexpand_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_userexpand_proto_rawDesc = []byte{
|
||||
0x0a, 0x10, 0x75, 0x73, 0x65, 0x72, 0x65, 0x78, 0x70, 0x61, 0x6e, 0x64, 0x2e, 0x70, 0x72, 0x6f,
|
||||
0x74, 0x6f, 0x22, 0x5e, 0x0a, 0x0c, 0x44, 0x42, 0x55, 0x73, 0x65, 0x72, 0x45, 0x78, 0x70, 0x61,
|
||||
0x6e, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02,
|
||||
0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x03, 0x75, 0x69, 0x64, 0x12, 0x2c, 0x0a, 0x11, 0x6c, 0x61, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64,
|
||||
0x6e, 0x6f, 0x74, 0x69, 0x66, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52,
|
||||
0x11, 0x6c, 0x61, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x74, 0x69,
|
||||
0x6d, 0x65, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x33,
|
||||
0x74, 0x6f, 0x22, 0x80, 0x01, 0x0a, 0x0c, 0x44, 0x42, 0x55, 0x73, 0x65, 0x72, 0x45, 0x78, 0x70,
|
||||
0x61, 0x6e, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x02, 0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x2c, 0x0a, 0x11, 0x6c, 0x61, 0x73, 0x74, 0x72, 0x65, 0x61,
|
||||
0x64, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03,
|
||||
0x52, 0x11, 0x6c, 0x61, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x74,
|
||||
0x69, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x43, 0x68, 0x61, 0x74, 0x43, 0x68, 0x61, 0x6e, 0x6e,
|
||||
0x65, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x43, 0x68, 0x61, 0x74, 0x43, 0x68,
|
||||
0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
Loading…
Reference in New Issue
Block a user