65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package modules
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
|
|
"github.com/liwei1dao/lego/base"
|
|
"github.com/liwei1dao/lego/core"
|
|
"github.com/liwei1dao/lego/core/cbase"
|
|
"github.com/liwei1dao/lego/sys/log"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type ModuleBase struct {
|
|
cbase.ModuleBase
|
|
service base.IRPCXService
|
|
}
|
|
|
|
func (this *ModuleBase) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
|
err = this.ModuleBase.Init(service, module, options)
|
|
this.service = service.(base.IRPCXService)
|
|
return
|
|
}
|
|
|
|
func (this *ModuleBase) SendMsgToUser(ServiceMethod string, msg proto.Message, user *pb.Cache_UserData) (err error) {
|
|
reply := &pb.RPCMessageReply{}
|
|
data, _ := proto.Marshal(msg)
|
|
if _, err = this.service.RpcGoById(user.GatewayServiceId, string(comm.Rpc_GatewayAgentSendMsg), context.Background(), &pb.AgentSendMessageReq{
|
|
UserSessionId: user.SessionId,
|
|
ServiceMethod: ServiceMethod,
|
|
Data: data,
|
|
}, reply); err != nil {
|
|
log.Errorf("SendMsgToUser%d:%s [%s] err:%v", user.UserData.UserId, user.SessionId, ServiceMethod, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *ModuleBase) SendMsgToUsers(ServiceMethod string, msg proto.Message, user ...*pb.Cache_UserData) (err error) {
|
|
var (
|
|
gateways map[string][]string = make(map[string][]string)
|
|
gateway []string
|
|
ok bool
|
|
)
|
|
for _, v := range user {
|
|
if gateway, ok = gateways[v.GatewayServiceId]; !ok {
|
|
gateway = make([]string, 0)
|
|
gateways[v.GatewayServiceId] = gateway
|
|
}
|
|
gateway = append(gateway, v.SessionId)
|
|
}
|
|
reply := &pb.RPCMessageReply{}
|
|
data, _ := proto.Marshal(msg)
|
|
for k, v := range gateways {
|
|
if _, err = this.service.RpcGoById(k, string(comm.Rpc_GatewayAgentSendMsg), context.Background(), &pb.BatchMessageReq{
|
|
UserSessionIds: v,
|
|
ServiceMethod: ServiceMethod,
|
|
Data: data,
|
|
}, reply); err != nil {
|
|
log.Errorf("SendMsgToUsers:%s->%s err:%v", k, ServiceMethod, err)
|
|
}
|
|
}
|
|
return
|
|
}
|