package comm import ( "context" "fmt" "go_dreamfactory/pb" "go_dreamfactory/lego/base" "go_dreamfactory/lego/sys/log" "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes" ) /* 用户会话对象 跨服操作用户的对象 */ func NewUserSession(service base.IRPCXService, ip, sessionId, gatewayServiceId string, uid string) IUserSession { return &UserSession{ IP: ip, SessionId: sessionId, GatewayServiceId: gatewayServiceId, UserId: uid, service: service, } } type UserSession struct { IP string SessionId string GatewayServiceId string //用户所在网关服务 UserId string service base.IRPCXService } //获取用户的会话id func (this *UserSession) GetSessionId() string { return this.SessionId } //获取用户的uid func (this *UserSession) GetUserId() string { return this.UserId } //获取用户的远程ip地址 func (this *UserSession) GetIP() string { return this.IP } //用户当先所在网关服务 func (this *UserSession) GetGatewayServiceId() string { return this.GatewayServiceId } //是否登录 func (this *UserSession) IsLogin() bool { return this.UserId != "" } //绑定uid 登录后操作 func (this *UserSession) Build(uid string) (err error) { reply := &pb.RPCMessageReply{} if err := this.service.RpcCallById(this.GatewayServiceId, string(Rpc_GatewayAgentBuild), context.Background(), &pb.AgentBuildReq{ UserSessionId: this.SessionId, UserId: uid, }, reply); err != nil { log.Errorf("UserSession:%s UserId:%s Build:%s err:%v", this.SessionId, this.UserId, err) } return } //解绑uid 注销和切换账号是处理 func (this *UserSession) UnBuild(ServiceMethod string, msg proto.Message) (err error) { reply := &pb.RPCMessageReply{} if err := this.service.RpcCallById(this.GatewayServiceId, string(Rpc_GatewayAgentUnBuild), context.Background(), &pb.AgentUnBuildReq{ UserSessionId: this.SessionId, }, reply); err != nil { log.Errorf("UserSession:%s UserId:%s UnBuild err:%v", this.SessionId, this.UserId, err) } return } //向用户发送消息 func (this *UserSession) SendMsg(mainType, subType string, code pb.ErrorCode, msg proto.Message) (err error) { reply := &pb.RPCMessageReply{} data, _ := ptypes.MarshalAny(msg) log.Debugf("SendMsg to SessionId:[%s] UserId:[%s] Code:%d Data: %v", this.UserId, code, msg) if err := this.service.RpcCallById(this.GatewayServiceId, string(Rpc_GatewayAgentSendMsg), context.Background(), &pb.AgentSendMessageReq{ UserSessionId: this.SessionId, MainType: mainType, SubType: subType, Code: code, Data: data, }, reply); err != nil { log.Errorf("UserSession:%s UserId:%s SendMsg:%s err:%v", this.SessionId, this.UserId, mainType, err) } return } //关闭用户连接对象 func (this *UserSession) Close() (err error) { reply := &pb.RPCMessageReply{} if err := this.service.RpcCallById(this.GatewayServiceId, string(Rpc_GatewayAgentSendMsg), context.Background(), &pb.AgentCloseeReq{ UserSessionId: this.SessionId, }, reply); err != nil { log.Errorf("UserSession:%s UserId:%d Close:%s err:%v", this.SessionId, this.UserId, err) } return } //打印日志需要 func (this *UserSession) ToString() string { return fmt.Sprintf("SessionId:%s UserId:%s GatewayServiceId:%s", this.SessionId, this.UserId, this.GatewayServiceId) }