robot统一通知处理

This commit is contained in:
zhaocy 2022-06-17 16:59:44 +08:00
parent 028727449b
commit cab6a2d07b
5 changed files with 48 additions and 10 deletions

View File

@ -33,7 +33,6 @@ func (r *Robot) BuildSecStr() string {
//处理登录请求
func (r *Robot) AccountLogin() {
//登录
// sec := r.BuildSecStr()
// log.Printf("client secret key:%s", sec)
loginReg := &pb.UserLoginReq{
Account: r.opts.Account,
@ -43,6 +42,7 @@ func (r *Robot) AccountLogin() {
head := &pb.UserMessage{
MainType: "user",
SubType: "login",
Sec: r.BuildSecStr(),
}
defer traceFunc(head.MainType, head.SubType, "0", loginReg)
err := r.SendToClient(head, loginReg)

26
cmd/robot/notify.go Normal file
View File

@ -0,0 +1,26 @@
package robot
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
)
//统一通知处理
func (r *Robot) handleNotifyMsg(msg *pb.UserMessage) {
switch msg.SubType {
case comm.SubType_ErrorNotify:
r.handleError(msg)
default:
fmt.Printf("subType: %s not define", msg.SubType)
}
}
//处理错误
func (r *Robot) handleError(msg *pb.UserMessage) {
rsp := &pb.ErrorNotify{}
if !comm.ProtoUnmarshal(msg, rsp) {
return
}
printReply(msg, rsp)
}

View File

@ -75,6 +75,8 @@ func (r *Robot) handleMsg(msg *pb.UserMessage) {
r.handleFriendMsg(msg)
case "pack":
r.handlePackMsg(msg)
case comm.MainType_Notify:
r.handleNotifyMsg(msg)
default:
log.Fatal("module route no exist")
}

View File

@ -8,7 +8,6 @@ import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
@ -22,6 +21,12 @@ const (
Service_Worker = "worker"
)
//ERR
const (
MainType_Notify = "notify" //通知
SubType_ErrorNotify = "errornotify" //错误通知
)
//模块名定义处
const (
SM_GateModule core.M_Modules = "gateway" //gate模块 网关服务模块

View File

@ -76,12 +76,15 @@ locp:
} else {
err = this.secAuth(msg)
if err == nil {
this.messageDistribution(msg)
if err := this.messageDistribution(msg); err != nil {
go this.Close()
break locp
}
} else {
data, _ := anypb.New(&pb.ErrorNotify{ReqMainType: msg.MainType, ReqSubType: msg.SubType, Code: pb.ErrorCode_SecKeyInvalid})
if err = this.WriteMsg(&pb.UserMessage{
MainType: "notify",
SubType: "errornotify",
MainType: comm.MainType_Notify,
SubType: comm.SubType_ErrorNotify,
Data: data,
}); err != nil {
go this.Close()
@ -201,7 +204,7 @@ func (this *Agent) Close() {
}
//分发用户消息
func (this *Agent) messageDistribution(msg *pb.UserMessage) {
func (this *Agent) messageDistribution(msg *pb.UserMessage) error {
reply := &pb.RPCMessageReply{}
log.Debugf("agent:%s uId:%s MessageDistribution msg:%s.%s", this.sessionId, this.uId, msg.MainType, msg.SubType)
if err := this.gateway.Service().RpcCall(context.Background(), comm.Service_Worker, string(comm.Rpc_GatewayRoute), &pb.AgentMessage{
@ -213,14 +216,16 @@ func (this *Agent) messageDistribution(msg *pb.UserMessage) {
Message: msg.Data,
}, reply); err != nil {
log.Errorf("agent:%s uId:%s MessageDistribution err:%v", this.sessionId, this.uId, err)
return
return err
}
if reply.Code != pb.ErrorCode_Success {
data, _ := anypb.New(&pb.ErrorNotify{ReqMainType: msg.MainType, ReqSubType: msg.SubType, Code: reply.Code})
this.WriteMsg(&pb.UserMessage{
MainType: "notify",
SubType: "errornotify",
err := this.WriteMsg(&pb.UserMessage{
MainType: comm.MainType_Notify,
SubType: comm.SubType_ErrorNotify,
Data: data,
})
return err
}
return nil
}