diff --git a/comm/core.go b/comm/core.go index 920a8f240..678dfa4a1 100644 --- a/comm/core.go +++ b/comm/core.go @@ -70,7 +70,8 @@ type IUserSession interface { UnBind() (err error) SendMsg(mainType, subType string, msg proto.Message) (err error) Polls() []*pb.UserMessage - Push() (err error) //警告 api传递过来的会话禁用此接口 + Push() (err error) //警告 api传递过来的会话禁用此接口 + SyncPush() (err error) //警告 api传递过来的会话禁用此接口 同步 Close() (err error) Reset() Clone() (session IUserSession) //克隆 diff --git a/comm/usersession.go b/comm/usersession.go index e7695a4eb..93c22aa6b 100644 --- a/comm/usersession.go +++ b/comm/usersession.go @@ -126,7 +126,7 @@ func (this *UserSession) SendMsg(mainType, subType string, msg proto.Message) (e // 关闭用户连接对象 func (this *UserSession) Close() (err error) { reply := &pb.RPCMessageReply{} - if err := this.service.RpcCall(context.Background(), fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentSendMsg), &pb.AgentCloseeReq{ + if err := this.service.RpcCall(context.Background(), fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentClose), &pb.AgentCloseeReq{ UserSessionId: this.SessionId, }, reply); err != nil { log.Errorf("Close UserSession:%s UserId:%s err:%v", this.SessionId, this.UserId, err) @@ -156,6 +156,20 @@ func (this *UserSession) Push() (err error) { return } +func (this *UserSession) SyncPush() (err error) { + // reply := &pb.RPCMessageReply{} + if len(this.msgqueue) > 0 { + if err = this.service.AcrossClusterRpcCall(context.Background(), this.ServiceTag, fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentSendMsg), &pb.AgentSendMessageReq{ + UserSessionId: this.SessionId, + Reply: this.msgqueue, + }, &pb.RPCMessageReply{}); err != nil { + log.Errorf("SendMsgToUsers:%v err:%v", this, err) + } + } + this.msgqueue = this.msgqueue[:0] + return +} + // 克隆 func (this *UserSession) Clone() (session IUserSession) { session = this.service.GetUserSession() diff --git a/modules/gateway/agentmgr_comp.go b/modules/gateway/agentmgr_comp.go index 0605734fe..19e32022b 100644 --- a/modules/gateway/agentmgr_comp.go +++ b/modules/gateway/agentmgr_comp.go @@ -199,6 +199,7 @@ func (this *AgentMgrComp) SendMsgToAllAgent(ctx context.Context, args *pb.BroadC func (this *AgentMgrComp) CloseAgent(ctx context.Context, args *pb.AgentCloseeReq, reply *pb.RPCMessageReply) error { if a, ok := this.agents.Load(args.UserSessionId); ok { a.(IAgent).Close() + this.agents.Delete(args.UserSessionId) } else { reply.ErrorData = &pb.ErrorData{ Code: pb.ErrorCode_UserSessionNobeing, diff --git a/modules/modulebase.go b/modules/modulebase.go index 43e7d0af9..48976d2ff 100644 --- a/modules/modulebase.go +++ b/modules/modulebase.go @@ -198,7 +198,7 @@ func (this *ModuleBase) SendMsgToUser(mainType, subType string, msg proto.Messag session := this.scomp.GetUserSession(user) session.SendMsg(mainType, subType, msg) err = session.Push() - this.scomp.PutUserSession(session) + this.PutUserSession(session) return } diff --git a/modules/user/api_login.go b/modules/user/api_login.go index 92d511573..9af69f361 100644 --- a/modules/user/api_login.go +++ b/modules/user/api_login.go @@ -70,16 +70,11 @@ func (this *apiComp) Login(session comm.IUserSession, req *pb.UserLoginReq) (err isNewUser = true } else { // 玩家是否已在线 - if cu := this.module.modelSession.getUserSession(user.Uid); cu != nil { - if cu.SessionId != "" { - // 通知先登录的玩家并解绑 - this.module.SendMsgToUser(string(this.module.GetType()), "othertermlogin", &pb.UserOtherTermLoginPush{Uid: cu.Uid}, cu.Uid) - if isession, ok := this.module.ModuleBase.GetUserSession(cu.Uid); ok { - if err := isession.UnBind(); err != nil { - this.module.Errorf("解绑失败 uid:%v sessionId:%v err:%v", cu.Uid, cu.SessionId, err) - } - } - } + if isession, ok := this.module.ModuleBase.GetUserSession(user.Uid); ok { + isession.SendMsg(string(this.module.GetType()), "othertermlogin", &pb.UserOtherTermLoginPush{Uid: user.Uid}) + isession.SyncPush() + isession.UnBind() + this.module.PutUserSession(isession) } } @@ -87,8 +82,9 @@ func (this *apiComp) Login(session comm.IUserSession, req *pb.UserLoginReq) (err err = session.Bind(user.Uid, this.service.GetId()) if err != nil { errdata = &pb.ErrorData{ - Code: pb.ErrorCode_BindUser, - Title: pb.ErrorCode_BindUser.ToString(), + Code: pb.ErrorCode_BindUser, + Title: pb.ErrorCode_BindUser.ToString(), + Message: err.Error(), } return }