From 0031d7bfe831f9d81e0ae02001e5e0160a139de4 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Tue, 19 Jul 2022 19:35:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E8=81=8A=E5=A4=A9=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E8=B7=A8=E6=9C=8D=E6=88=90=E5=91=98=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E7=BB=B4=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego/base/core.go | 2 +- modules/chat/api_crosschannel.go | 13 ++++++++++++- modules/chat/api_spansend.go | 17 +++++++++------- modules/chat/modelChat.go | 33 +++++++++++++++++++++++++++++--- modules/chat/module.go | 27 ++++++++++++++++++++++++++ modules/gateway/agentmgr_comp.go | 9 +++++++++ modules/gateway/options.go | 5 +++-- modules/modulebase.go | 26 +++++++++++++++---------- pb/proto/userexpand.proto | 3 ++- pb/userexpand.pb.go | 26 +++++++++++++++++-------- 10 files changed, 128 insertions(+), 33 deletions(-) diff --git a/lego/base/core.go b/lego/base/core.go index 08f906a53..714cbdd5b 100644 --- a/lego/base/core.go +++ b/lego/base/core.go @@ -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) } diff --git a/modules/chat/api_crosschannel.go b/modules/chat/api_crosschannel.go index 818d29adc..20a0f1ef5 100644 --- a/modules/chat/api_crosschannel.go +++ b/modules/chat/api_crosschannel.go @@ -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 } diff --git a/modules/chat/api_spansend.go b/modules/chat/api_spansend.go index faa73ec07..8e8bd1f46 100644 --- a/modules/chat/api_spansend.go +++ b/modules/chat/api_spansend.go @@ -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 diff --git a/modules/chat/modelChat.go b/modules/chat/modelChat.go index c6e05625f..a70fb61b5 100644 --- a/modules/chat/modelChat.go +++ b/modules/chat/modelChat.go @@ -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 { diff --git a/modules/chat/module.go b/modules/chat/module.go index 6f93e3186..57b46a08a 100644 --- a/modules/chat/module.go +++ b/modules/chat/module.go @@ -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 ( diff --git a/modules/gateway/agentmgr_comp.go b/modules/gateway/agentmgr_comp.go index af1cbfc91..f40eb55b4 100644 --- a/modules/gateway/agentmgr_comp.go +++ b/modules/gateway/agentmgr_comp.go @@ -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) + } } } diff --git a/modules/gateway/options.go b/modules/gateway/options.go index 6fb187a13..e47f55b3e 100644 --- a/modules/gateway/options.go +++ b/modules/gateway/options.go @@ -12,8 +12,9 @@ import ( type ( Options struct { modules.Options - GinDebug bool //web引擎日志开关 - ListenPort int //websocket 监听端口 + GinDebug bool //web引擎日志开关 + ListenPort int //websocket 监听端口 + SpanServiceTag string //跨服集群 } ) diff --git a/modules/modulebase.go b/modules/modulebase.go index d3a7f1209..1379060bb 100644 --- a/modules/modulebase.go +++ b/modules/modulebase.go @@ -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 diff --git a/pb/proto/userexpand.proto b/pb/proto/userexpand.proto index d39b2e64c..e484b3e42 100644 --- a/pb/proto/userexpand.proto +++ b/pb/proto/userexpand.proto @@ -7,4 +7,5 @@ message DBUserExpand { string id = 1; //主键id string uid = 2; //用户id int64 lastreadnotiftime = 3; //最后阅读公告时间 -} \ No newline at end of file + int32 ChatChannel = 4; //跨服聊天频道 + } \ No newline at end of file diff --git a/pb/userexpand.pb.go b/pb/userexpand.pb.go index c84ca5f69..be89f14f8 100644 --- a/pb/userexpand.pb.go +++ b/pb/userexpand.pb.go @@ -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 (