跨服session

This commit is contained in:
wh_zcy 2022-10-18 10:15:02 +08:00
parent 85bb6f5391
commit f51f492164
6 changed files with 149 additions and 53 deletions

View File

@ -67,8 +67,10 @@ type (
//玩家 //玩家
IUser interface { IUser interface {
//获取用户数据 //获取本服用户数据
GetUser(uid string) *pb.DBUser GetUser(uid string) *pb.DBUser
// 获取跨服用户数据
GetCrossUser(uid string) (*pb.DBUser, error)
//获取用户回话 //获取用户回话
GetUserSession(uid string) *pb.CacheUser GetUserSession(uid string) *pb.CacheUser
//查询用户属性值 例如 金币 经验 //查询用户属性值 例如 金币 经验

View File

@ -38,16 +38,20 @@ func (this *apiComp) Init(service core.IService, module core.IModule, comp core.
} }
func (this *apiComp) setDefaultFriendUserBaseInfo(userId string) *pb.FriendBase { func (this *apiComp) setDefaultFriendUserBaseInfo(userId string) *pb.FriendBase {
user := this.moduleFriend.ModuleUser.GetUser(userId) if user, err := this.moduleFriend.ModuleUser.GetCrossUser(userId); err != nil {
if user != nil { return nil
return &pb.FriendBase{ } else {
ServerId: user.Sid, if user != nil {
UserId: userId, return &pb.FriendBase{
NickName: user.Name, ServerId: user.Sid,
Level: user.Lv, UserId: userId,
Avatar: user.Avatar, NickName: user.Name,
OfflineTime: user.Offlinetime, Level: user.Lv,
Avatar: user.Avatar,
OfflineTime: user.Offlinetime,
}
} }
} }
return nil return nil
} }

View File

@ -71,12 +71,6 @@ func (this *apiComp) Randlist(session comm.IUserSession, req *pb.FriendRandlistR
continue continue
} }
// // 获取在线好友的信息
// user := this.moduleFriend.ModuleUser.GetUser(uid)
// if user == nil {
// continue
// }
base := this.setDefaultFriendUserBaseInfo(uid) base := this.setDefaultFriendUserBaseInfo(uid)
if base == nil { if base == nil {
continue continue

View File

@ -1,6 +1,7 @@
package user package user
import ( import (
"fmt"
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
@ -19,7 +20,12 @@ func (this *ModelSession) Init(service core.IService, module core.IModule, comp
this.module = module.(*User) this.module = module.(*User)
this.TableName = comm.TableSession this.TableName = comm.TableSession
this.Expired = 0 //不自动过期 this.Expired = 0 //不自动过期
this.clean() return
}
func (this *ModelSession) Start() (err error) {
this.MCompModel.Start()
this.clean(this.module.service.GetTag())
return return
} }
@ -45,15 +51,12 @@ func (this *ModelSession) setUserSession(uid string, session comm.IUserSession)
log.Debugf("setUserSession err:%v", err) log.Debugf("setUserSession err:%v", err)
return return
} }
// this.module.service.AcrossClusterRpcGo(context.TODO(),
// this.module.service.GetTag(), comm.Service_Worker, serviceMethod string, args interface{}, reply interface{})
return return
} }
// 启动时清理session // 启动时清理session
func (this *ModelSession) clean() { func (this *ModelSession) clean(key string) {
keys, err := this.Redis.Keys("session:*") keys, err := this.Redis.Keys(fmt.Sprintf("session:%s-", key))
if err != nil { if err != nil {
log.Errorf("redis keys err:%v", err) log.Errorf("redis keys err:%v", err)
return return

View File

@ -7,6 +7,7 @@ import (
"go_dreamfactory/modules" "go_dreamfactory/modules"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/sys/db" "go_dreamfactory/sys/db"
"go_dreamfactory/utils"
"go_dreamfactory/lego/base" "go_dreamfactory/lego/base"
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
@ -17,7 +18,10 @@ import (
) )
const ( const (
// 跨服玩家
Rpc_GetAllOnlineUser string = "Rpc_GetAllOnlineUser" Rpc_GetAllOnlineUser string = "Rpc_GetAllOnlineUser"
// 跨服用户
Rpc_GetCrossUser string = "Rpc_GetCrossUser"
) )
var _ comm.IUser = (*User)(nil) var _ comm.IUser = (*User)(nil)
@ -51,7 +55,8 @@ func (this *User) Init(service core.IService, module core.IModule, options core.
func (this *User) Start() (err error) { func (this *User) Start() (err error) {
err = this.ModuleBase.Start() err = this.ModuleBase.Start()
event.RegisterGO(comm.EventUserOffline, this.CleanSession) event.RegisterGO(comm.EventUserOffline, this.CleanSession)
this.service.RegisterFunctionName(Rpc_GetAllOnlineUser, this.GetAllOnlineUser) this.service.RegisterFunctionName(Rpc_GetAllOnlineUser, this.RpcGetAllOnlineUser)
this.service.RegisterFunctionName(Rpc_GetCrossUser, this.RpcGetCrossUser)
return return
} }
@ -73,6 +78,14 @@ func (this *User) GetUser(uid string) *pb.DBUser {
return user return user
} }
// 获取跨服用户数据
func (this *User) GetCrossUser(uid string) (*pb.DBUser, error) {
reply := &pb.DBUser{}
err := this.service.AcrossClusterRpcCall(context.Background(), this.GetCrossTag(),
comm.Service_Worker, Rpc_GetCrossUser, &pb.UIdReq{Uid: uid}, reply)
return reply, err
}
//获取用户会话 //获取用户会话
func (this *User) GetUserSession(uid string) *pb.CacheUser { func (this *User) GetUserSession(uid string) *pb.CacheUser {
return this.modelSession.getUserSession(uid) return this.modelSession.getUserSession(uid)
@ -101,7 +114,7 @@ func (this *User) UserOnlineList() ([]*pb.CacheUser, error) {
// 跨服玩家列表 // 跨服玩家列表
func (this *User) CrossUserOnlineList() ([]*pb.CacheUser, error) { func (this *User) CrossUserOnlineList() ([]*pb.CacheUser, error) {
reply := &pb.UserOnlineResp{} reply := &pb.UserOnlineResp{}
err := this.service.AcrossClusterRpcCall(context.Background(), this.service.GetTag(), err := this.service.AcrossClusterRpcCall(context.Background(), this.GetCrossTag(),
comm.Service_Worker, Rpc_GetAllOnlineUser, nil, reply) comm.Service_Worker, Rpc_GetAllOnlineUser, nil, reply)
return reply.Users, err return reply.Users, err
} }
@ -275,8 +288,8 @@ func (this *User) ChangeUserExpand(uid string, value map[string]interface{}) err
return this.modelExpand.ChangeUserExpand(uid, value) return this.modelExpand.ChangeUserExpand(uid, value)
} }
func (this *User) GetAllOnlineUser(ctx context.Context, args *pb.EmptyReq, reply *pb.UserOnlineResp) error { func (this *User) RpcGetAllOnlineUser(ctx context.Context, args *pb.EmptyReq, reply *pb.UserOnlineResp) error {
conn, err := db.Cross() conn, err := db.Local()
if err != nil { if err != nil {
log.Errorf("cross db err: %v", err) log.Errorf("cross db err: %v", err)
return err return err
@ -289,3 +302,22 @@ func (this *User) GetAllOnlineUser(ctx context.Context, args *pb.EmptyReq, reply
reply.Users = cache reply.Users = cache
return nil return nil
} }
func (this *User) RpcGetCrossUser(ctx context.Context, req *pb.UIdReq, reply *pb.DBUser) error {
sid, _, ok := utils.UIdSplit(req.Uid)
if !ok {
return errors.New("sid split error")
}
conn, err := db.ServerDBConn(sid)
if err != nil {
log.Errorf("cross db err: %v", err)
return err
}
model := db.NewDBModel(comm.TableUser, 0, conn)
if err := model.Get(req.Uid, reply); err != nil {
return err
}
return nil
}

View File

@ -1020,6 +1020,53 @@ func (x *RtaskParam) GetParam3() int32 {
return 0 return 0
} }
type UIdReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Uid string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid"`
}
func (x *UIdReq) Reset() {
*x = UIdReq{}
if protoimpl.UnsafeEnabled {
mi := &file_comm_proto_msgTypes[14]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UIdReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UIdReq) ProtoMessage() {}
func (x *UIdReq) ProtoReflect() protoreflect.Message {
mi := &file_comm_proto_msgTypes[14]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UIdReq.ProtoReflect.Descriptor instead.
func (*UIdReq) Descriptor() ([]byte, []int) {
return file_comm_proto_rawDescGZIP(), []int{14}
}
func (x *UIdReq) GetUid() string {
if x != nil {
return x.Uid
}
return ""
}
type EmptyReq struct { type EmptyReq struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
@ -1029,7 +1076,7 @@ type EmptyReq struct {
func (x *EmptyReq) Reset() { func (x *EmptyReq) Reset() {
*x = EmptyReq{} *x = EmptyReq{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_comm_proto_msgTypes[14] mi := &file_comm_proto_msgTypes[15]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@ -1042,7 +1089,7 @@ func (x *EmptyReq) String() string {
func (*EmptyReq) ProtoMessage() {} func (*EmptyReq) ProtoMessage() {}
func (x *EmptyReq) ProtoReflect() protoreflect.Message { func (x *EmptyReq) ProtoReflect() protoreflect.Message {
mi := &file_comm_proto_msgTypes[14] mi := &file_comm_proto_msgTypes[15]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@ -1055,7 +1102,7 @@ func (x *EmptyReq) ProtoReflect() protoreflect.Message {
// Deprecated: Use EmptyReq.ProtoReflect.Descriptor instead. // Deprecated: Use EmptyReq.ProtoReflect.Descriptor instead.
func (*EmptyReq) Descriptor() ([]byte, []int) { func (*EmptyReq) Descriptor() ([]byte, []int) {
return file_comm_proto_rawDescGZIP(), []int{14} return file_comm_proto_rawDescGZIP(), []int{15}
} }
type EmptyResp struct { type EmptyResp struct {
@ -1067,7 +1114,7 @@ type EmptyResp struct {
func (x *EmptyResp) Reset() { func (x *EmptyResp) Reset() {
*x = EmptyResp{} *x = EmptyResp{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_comm_proto_msgTypes[15] mi := &file_comm_proto_msgTypes[16]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@ -1080,7 +1127,7 @@ func (x *EmptyResp) String() string {
func (*EmptyResp) ProtoMessage() {} func (*EmptyResp) ProtoMessage() {}
func (x *EmptyResp) ProtoReflect() protoreflect.Message { func (x *EmptyResp) ProtoReflect() protoreflect.Message {
mi := &file_comm_proto_msgTypes[15] mi := &file_comm_proto_msgTypes[16]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@ -1093,7 +1140,7 @@ func (x *EmptyResp) ProtoReflect() protoreflect.Message {
// Deprecated: Use EmptyResp.ProtoReflect.Descriptor instead. // Deprecated: Use EmptyResp.ProtoReflect.Descriptor instead.
func (*EmptyResp) Descriptor() ([]byte, []int) { func (*EmptyResp) Descriptor() ([]byte, []int) {
return file_comm_proto_rawDescGZIP(), []int{15} return file_comm_proto_rawDescGZIP(), []int{16}
} }
var File_comm_proto protoreflect.FileDescriptor var File_comm_proto protoreflect.FileDescriptor
@ -1210,14 +1257,15 @@ var file_comm_proto_rawDesc = []byte{
0x61, 0x72, 0x61, 0x6d, 0x31, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x32, 0x18, 0x61, 0x72, 0x61, 0x6d, 0x31, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x32, 0x18,
0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x32, 0x12, 0x16, 0x0a, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x32, 0x12, 0x16, 0x0a,
0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x33, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x33, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70,
0x61, 0x72, 0x61, 0x6d, 0x33, 0x22, 0x0a, 0x0a, 0x08, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x61, 0x72, 0x61, 0x6d, 0x33, 0x22, 0x1a, 0x0a, 0x06, 0x55, 0x49, 0x64, 0x52, 0x65, 0x71, 0x12,
0x71, 0x22, 0x0b, 0x0a, 0x09, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x73, 0x70, 0x2a, 0x43, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69,
0x0a, 0x12, 0x48, 0x65, 0x72, 0x6f, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, 0x64, 0x22, 0x0a, 0x0a, 0x08, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x71, 0x22, 0x0b, 0x0a,
0x54, 0x79, 0x70, 0x65, 0x12, 0x06, 0x0a, 0x02, 0x48, 0x70, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x09, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x73, 0x70, 0x2a, 0x43, 0x0a, 0x12, 0x48, 0x65,
0x41, 0x74, 0x6b, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x44, 0x65, 0x66, 0x10, 0x02, 0x12, 0x09, 0x72, 0x6f, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, 0x54, 0x79, 0x70, 0x65,
0x0a, 0x05, 0x53, 0x70, 0x65, 0x65, 0x64, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x72, 0x69, 0x12, 0x06, 0x0a, 0x02, 0x48, 0x70, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x74, 0x6b, 0x10,
0x74, 0x10, 0x04, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x44, 0x65, 0x66, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x53, 0x70,
0x74, 0x6f, 0x33, 0x65, 0x65, 0x64, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x72, 0x69, 0x74, 0x10, 0x04, 0x42,
0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@ -1233,7 +1281,7 @@ func file_comm_proto_rawDescGZIP() []byte {
} }
var file_comm_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_comm_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_comm_proto_msgTypes = make([]protoimpl.MessageInfo, 16) var file_comm_proto_msgTypes = make([]protoimpl.MessageInfo, 17)
var file_comm_proto_goTypes = []interface{}{ var file_comm_proto_goTypes = []interface{}{
(HeroAttributesType)(0), // 0: HeroAttributesType (HeroAttributesType)(0), // 0: HeroAttributesType
(*UserMessage)(nil), // 1: UserMessage (*UserMessage)(nil), // 1: UserMessage
@ -1250,20 +1298,21 @@ var file_comm_proto_goTypes = []interface{}{
(*UserAssets)(nil), // 12: UserAssets (*UserAssets)(nil), // 12: UserAssets
(*TaskParam)(nil), // 13: TaskParam (*TaskParam)(nil), // 13: TaskParam
(*RtaskParam)(nil), // 14: RtaskParam (*RtaskParam)(nil), // 14: RtaskParam
(*EmptyReq)(nil), // 15: EmptyReq (*UIdReq)(nil), // 15: UIdReq
(*EmptyResp)(nil), // 16: EmptyResp (*EmptyReq)(nil), // 16: EmptyReq
(*anypb.Any)(nil), // 17: google.protobuf.Any (*EmptyResp)(nil), // 17: EmptyResp
(ErrorCode)(0), // 18: ErrorCode (*anypb.Any)(nil), // 18: google.protobuf.Any
(ErrorCode)(0), // 19: ErrorCode
} }
var file_comm_proto_depIdxs = []int32{ var file_comm_proto_depIdxs = []int32{
17, // 0: UserMessage.data:type_name -> google.protobuf.Any 18, // 0: UserMessage.data:type_name -> google.protobuf.Any
17, // 1: AgentMessage.Message:type_name -> google.protobuf.Any 18, // 1: AgentMessage.Message:type_name -> google.protobuf.Any
18, // 2: RPCMessageReply.Code:type_name -> ErrorCode 19, // 2: RPCMessageReply.Code:type_name -> ErrorCode
17, // 3: RPCMessageReply.ErrorData:type_name -> google.protobuf.Any 18, // 3: RPCMessageReply.ErrorData:type_name -> google.protobuf.Any
1, // 4: RPCMessageReply.Reply:type_name -> UserMessage 1, // 4: RPCMessageReply.Reply:type_name -> UserMessage
1, // 5: AgentSendMessageReq.Reply:type_name -> UserMessage 1, // 5: AgentSendMessageReq.Reply:type_name -> UserMessage
17, // 6: BatchMessageReq.Data:type_name -> google.protobuf.Any 18, // 6: BatchMessageReq.Data:type_name -> google.protobuf.Any
17, // 7: BroadCastMessageReq.Data:type_name -> google.protobuf.Any 18, // 7: BroadCastMessageReq.Data:type_name -> google.protobuf.Any
8, // [8:8] is the sub-list for method output_type 8, // [8:8] is the sub-list for method output_type
8, // [8:8] is the sub-list for method input_type 8, // [8:8] is the sub-list for method input_type
8, // [8:8] is the sub-list for extension type_name 8, // [8:8] is the sub-list for extension type_name
@ -1447,7 +1496,7 @@ func file_comm_proto_init() {
} }
} }
file_comm_proto_msgTypes[14].Exporter = func(v interface{}, i int) interface{} { file_comm_proto_msgTypes[14].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*EmptyReq); i { switch v := v.(*UIdReq); i {
case 0: case 0:
return &v.state return &v.state
case 1: case 1:
@ -1459,6 +1508,18 @@ func file_comm_proto_init() {
} }
} }
file_comm_proto_msgTypes[15].Exporter = func(v interface{}, i int) interface{} { file_comm_proto_msgTypes[15].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*EmptyReq); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_comm_proto_msgTypes[16].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*EmptyResp); i { switch v := v.(*EmptyResp); i {
case 0: case 0:
return &v.state return &v.state
@ -1477,7 +1538,7 @@ func file_comm_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_comm_proto_rawDesc, RawDescriptor: file_comm_proto_rawDesc,
NumEnums: 1, NumEnums: 1,
NumMessages: 16, NumMessages: 17,
NumExtensions: 0, NumExtensions: 0,
NumServices: 0, NumServices: 0,
}, },