添加client测试端

This commit is contained in:
liuxiaobo 2025-05-29 20:23:33 +08:00
parent b3902bfafe
commit 7b18e9a8b6

View File

@ -4,37 +4,32 @@ import (
"fmt" "fmt"
"game/common/proto/pb" "game/common/proto/pb"
"game/test/client/config" "game/test/client/config"
"github.com/fox/fox/ipb"
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/fox/fox/processor" "github.com/fox/fox/processor"
"github.com/fox/fox/service"
"github.com/fox/fox/ws" "github.com/fox/fox/ws"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
) )
var Chat []*ClientService var Clients []*ClientService
type ClientService struct { type ClientService struct {
*service.BaseService
client *ws.Client client *ws.Client
processor *processor.Processor processor *processor.Processor
userId int64
} }
func Init() { func Init() {
for i := 0; i < config.Command.ServiceNum; i++ { for i := 0; i < config.Command.ServiceNum; i++ {
sid := config.Command.ServiceId + i sid := config.Command.ServiceId + i
if srv := newClientService(sid); srv != nil { if srv := newClientService(sid); srv != nil {
Chat = append(Chat, srv) Clients = append(Clients, srv)
} }
} }
} }
func Stop() { func Stop() {
for _, srv := range Chat { for _, s := range Clients {
srv.NotifyStop() s.client.Stop()
}
for _, srv := range Chat {
srv.WaitStop()
} }
} }
@ -42,8 +37,6 @@ func newClientService(serviceId int) *ClientService {
var err error var err error
s := new(ClientService) s := new(ClientService)
sName := fmt.Sprintf("%v-%d", "client", serviceId)
s.BaseService = service.NewBaseService("client", sName, s, s)
size := len(config.Cfg.Special.Address) size := len(config.Cfg.Special.Address)
addr := config.Cfg.Special.Address[serviceId%size] addr := config.Cfg.Special.Address[serviceId%size]
if s.client, err = ws.NewClient(fmt.Sprintf("ws://%v", addr), s); err != nil { if s.client, err = ws.NewClient(fmt.Sprintf("ws://%v", addr), s); err != nil {
@ -57,7 +50,6 @@ func newClientService(serviceId int) *ClientService {
func (s *ClientService) OnInit() { func (s *ClientService) OnInit() {
s.client.Start() s.client.Start()
s.BaseService.Run()
log.Debug("onInit") log.Debug("onInit")
} }
@ -65,11 +57,6 @@ func (s *ClientService) CanStop() bool {
return true return true
} }
func (s *ClientService) OnStop() {
s.client.Stop()
log.Debug("OnStop")
}
// 处理其它服发送过来的消息 // 处理其它服发送过来的消息
func (s *ClientService) OnMessage(data []byte) error { func (s *ClientService) OnMessage(data []byte) error {
var cMsg = &pb.ClientMsg{} var cMsg = &pb.ClientMsg{}
@ -86,13 +73,20 @@ func (s *ClientService) OnMessage(data []byte) error {
} }
// 向内部服务发送消息 // 向内部服务发送消息
func (s *ClientService) SendServiceData(topic string, userId int64, msgId int32, data []byte) { func (s *ClientService) SendData(serviceTypeId pb.ServiceTypeId, msgId int32, data []byte) {
iMsg := ipb.MakeMsg(s.Name(), 0, userId, msgId, data) msg := &pb.ClientMsg{
_ = s.Send(topic, iMsg) ServiceTid: serviceTypeId,
ServiceName: "",
UserId: s.userId,
MsgId: msgId,
Data: data,
}
d, _ := proto.Marshal(msg)
s.client.SendMsg(d)
} }
// 向内部服务发送消息 // 向内部服务发送消息
func (s *ClientService) SendServiceMsg(topic string, userId int64, msgId int32, msg proto.Message) { func (s *ClientService) SendMsg(serviceTypeId pb.ServiceTypeId, msgId int32, msg proto.Message) {
data, _ := proto.Marshal(msg) data, _ := proto.Marshal(msg)
s.SendServiceData(topic, userId, msgId, data) s.SendData(serviceTypeId, msgId, data)
} }