处理离线用户消息发送

This commit is contained in:
liwei 2023-07-14 18:55:56 +08:00
parent ae792300ad
commit 51cc35d65f

View File

@ -33,7 +33,7 @@ type UserSession struct {
msgqueue []*pb.UserMessage msgqueue []*pb.UserMessage
} }
//重置 // 重置
func (this *UserSession) SetSession(ip, sessionId, stag, sid, uid string) { func (this *UserSession) SetSession(ip, sessionId, stag, sid, uid string) {
this.IP = ip this.IP = ip
this.SessionId = sessionId this.SessionId = sessionId
@ -43,7 +43,7 @@ func (this *UserSession) SetSession(ip, sessionId, stag, sid, uid string) {
this.msgqueue = this.msgqueue[:0] this.msgqueue = this.msgqueue[:0]
} }
//重置 // 重置
func (this *UserSession) Reset() { func (this *UserSession) Reset() {
this.IP = "" this.IP = ""
this.SessionId = "" this.SessionId = ""
@ -52,39 +52,39 @@ func (this *UserSession) Reset() {
this.msgqueue = this.msgqueue[:0] this.msgqueue = this.msgqueue[:0]
} }
//获取用户的会话id // 获取用户的会话id
func (this *UserSession) GetSessionId() string { func (this *UserSession) GetSessionId() string {
return this.SessionId return this.SessionId
} }
//获取用户的uid // 获取用户的uid
func (this *UserSession) GetUserId() string { func (this *UserSession) GetUserId() string {
return this.UserId return this.UserId
} }
//获取用户的远程ip地址 // 获取用户的远程ip地址
func (this *UserSession) GetIP() string { func (this *UserSession) GetIP() string {
return this.IP return this.IP
} }
//会话所在集群 // 会话所在集群
func (this *UserSession) GetServiecTag() string { func (this *UserSession) GetServiecTag() string {
return this.ServiceTag return this.ServiceTag
} }
//用户当先所在网关服务 // 用户当先所在网关服务
func (this *UserSession) GetGatewayServiceId() string { func (this *UserSession) GetGatewayServiceId() string {
return this.GatewayServiceId return this.GatewayServiceId
} }
//是否登录 // 是否登录
func (this *UserSession) IsLogin() bool { func (this *UserSession) IsLogin() bool {
return this.UserId != "" return this.UserId != ""
} }
///绑定uid 登录后操作 // /绑定uid 登录后操作
///uid 用户id // /uid 用户id
///wokerId 用户绑定worker服务id // /wokerId 用户绑定worker服务id
func (this *UserSession) Bind(uid string, wokerId string) (err error) { func (this *UserSession) Bind(uid string, wokerId string) (err error) {
reply := &pb.RPCMessageReply{} reply := &pb.RPCMessageReply{}
if err = this.service.RpcCall(context.Background(), fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentBind), &pb.AgentBuildReq{ if err = this.service.RpcCall(context.Background(), fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentBind), &pb.AgentBuildReq{
@ -99,7 +99,7 @@ func (this *UserSession) Bind(uid string, wokerId string) (err error) {
return return
} }
//解绑uid 注销和切换账号是处理 // 解绑uid 注销和切换账号是处理
func (this *UserSession) UnBind() (err error) { func (this *UserSession) UnBind() (err error) {
reply := &pb.RPCMessageReply{} reply := &pb.RPCMessageReply{}
if err = this.service.RpcCall(context.Background(), fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentUnBind), &pb.AgentUnBuildReq{ if err = this.service.RpcCall(context.Background(), fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentUnBind), &pb.AgentUnBuildReq{
@ -112,7 +112,7 @@ func (this *UserSession) UnBind() (err error) {
return return
} }
//向用户发送消息 // 向用户发送消息
func (this *UserSession) SendMsg(mainType, subType string, msg proto.Message) (err error) { func (this *UserSession) SendMsg(mainType, subType string, msg proto.Message) (err error) {
// log.Debugf("SendMsg to UserId:[%s] Data: %v", this.UserId, msg) // log.Debugf("SendMsg to UserId:[%s] Data: %v", this.UserId, msg)
data, _ := anypb.New(msg) data, _ := anypb.New(msg)
@ -124,7 +124,7 @@ func (this *UserSession) SendMsg(mainType, subType string, msg proto.Message) (e
return return
} }
//关闭用户连接对象 // 关闭用户连接对象
func (this *UserSession) Close() (err error) { func (this *UserSession) Close() (err error) {
reply := &pb.RPCMessageReply{} 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_GatewayAgentSendMsg), &pb.AgentCloseeReq{
@ -135,17 +135,17 @@ func (this *UserSession) Close() (err error) {
return return
} }
//清空消息队列 // 清空消息队列
func (this *UserSession) Polls() []*pb.UserMessage { func (this *UserSession) Polls() []*pb.UserMessage {
msgs := this.msgqueue msgs := this.msgqueue
this.msgqueue = this.msgqueue[:0] this.msgqueue = this.msgqueue[:0]
return msgs return msgs
} }
//推送消息到用户 // 推送消息到用户
func (this *UserSession) Push() (err error) { func (this *UserSession) Push() (err error) {
// reply := &pb.RPCMessageReply{} // reply := &pb.RPCMessageReply{}
if len(this.msgqueue) > 0 { if this.ServiceTag != "" && this.SessionId != "" && len(this.msgqueue) > 0 {
if _, err = this.service.AcrossClusterRpcGo(context.Background(), this.ServiceTag, fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentSendMsg), &pb.AgentSendMessageReq{ if _, err = this.service.AcrossClusterRpcGo(context.Background(), this.ServiceTag, fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentSendMsg), &pb.AgentSendMessageReq{
UserSessionId: this.SessionId, UserSessionId: this.SessionId,
Reply: this.msgqueue, Reply: this.msgqueue,
@ -157,7 +157,7 @@ func (this *UserSession) Push() (err error) {
return return
} }
//打印日志需要 // 打印日志需要
func (this *UserSession) ToString() string { func (this *UserSession) ToString() string {
return fmt.Sprintf("SessionId:%s UserId:%s GatewayServiceId:%s", this.SessionId, this.UserId, this.GatewayServiceId) return fmt.Sprintf("SessionId:%s UserId:%s GatewayServiceId:%s", this.SessionId, this.UserId, this.GatewayServiceId)
} }