添加client测试端
This commit is contained in:
parent
a68dbdd535
commit
b3902bfafe
29
test/client/cmd/cmd.go
Normal file
29
test/client/cmd/cmd.go
Normal file
@ -0,0 +1,29 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"game/test/client/config"
|
||||
"game/test/client/server"
|
||||
"github.com/fox/fox/log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func initRepo() {
|
||||
//model.InitRedis()
|
||||
}
|
||||
|
||||
func Run(GitCommit, GitBranch, BuildDate string) {
|
||||
config.LoadConfig(GitCommit, GitBranch, BuildDate)
|
||||
log.Info(fmt.Sprintf("版本分支:%v,hash值:%v,编译时间:%v", GitBranch, GitCommit, BuildDate))
|
||||
initRepo()
|
||||
|
||||
server.Init()
|
||||
// 截获 SIGINT 和 SIGTERM 信号
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
sig := <-c
|
||||
server.Stop()
|
||||
log.Info(fmt.Sprintf("received %s, initiating shutdown...", sig))
|
||||
}
|
41
test/client/config/config.go
Normal file
41
test/client/config/config.go
Normal file
@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"game/common/config"
|
||||
"github.com/fox/fox/db"
|
||||
"github.com/fox/fox/log"
|
||||
)
|
||||
|
||||
const (
|
||||
gateAddress1 = "114.132.124.145:5100"
|
||||
gateAddress2 = "114.132.124.145:5101"
|
||||
)
|
||||
|
||||
var Command *config.Command
|
||||
var Cfg *config.Common[ClientConfig]
|
||||
|
||||
type ClientConfig struct {
|
||||
Address []string `json:"address"` // 网关地址
|
||||
}
|
||||
|
||||
func initLog() {
|
||||
log.Open("client.log", log.DebugL)
|
||||
}
|
||||
|
||||
func LoadConfig(GitCommit, GitBranch, BuildDate string) {
|
||||
Command = config.ParseCommand()
|
||||
initLog()
|
||||
rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return
|
||||
}
|
||||
defer func() { _ = rdb.Close() }()
|
||||
Cfg, err = config.LoadCommonConfig[ClientConfig](rdb, GitCommit, GitBranch, BuildDate)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return
|
||||
}
|
||||
Cfg.Special.Address = append(Cfg.Special.Address, gateAddress1, gateAddress2)
|
||||
log.DebugF("load common config success")
|
||||
}
|
24
test/client/main.go
Normal file
24
test/client/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"game/test/client/cmd"
|
||||
"github.com/fox/fox/ksync"
|
||||
"github.com/fox/fox/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
GitCommit = "unknown"
|
||||
GitBranch = "unknown"
|
||||
BuildDate = "unknown"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tm, err := time.Parse("20060102150405", BuildDate)
|
||||
if err == nil {
|
||||
BuildDate = tm.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
ksync.RunSafe(func() {
|
||||
cmd.Run(GitBranch, GitCommit, BuildDate)
|
||||
}, func() { log.ErrorF("reset run") })
|
||||
}
|
19
test/client/model/db.go
Normal file
19
test/client/model/db.go
Normal file
@ -0,0 +1,19 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"game/test/client/config"
|
||||
"github.com/fox/fox/db"
|
||||
"github.com/fox/fox/log"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
var UserRedis *redis.Client
|
||||
var err error
|
||||
|
||||
func InitRedis() {
|
||||
UserRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, 0)
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
30
test/client/server/processor.go
Normal file
30
test/client/server/processor.go
Normal file
@ -0,0 +1,30 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"game/common/proto/pb"
|
||||
"github.com/fox/fox/processor"
|
||||
)
|
||||
|
||||
func (s *ClientService) initProcessor() {
|
||||
s.processor.RegisterMessages(processor.RegisterMetas{
|
||||
pb.MsgId_C2SChatId: {pb.C2SChat{}, s.onChat},
|
||||
})
|
||||
}
|
||||
|
||||
// 收到登陆成功消息,判断是否顶号
|
||||
func (s *ClientService) onChat(uid int64, msg *pb.C2SChat) {
|
||||
_ = uid
|
||||
_ = msg
|
||||
//switch msg.Type {
|
||||
//case pb.ChatType_CT_Private:
|
||||
// sName, err := s.bindService.FindServiceName(msg.DstUser.UserId, pb.ServiceTypeId_STI_Gate)
|
||||
// if err != nil {
|
||||
// log.DebugF("find user:%v in gate err: %v", uid, err)
|
||||
// return
|
||||
// }
|
||||
// s.SendServiceMsg(service.TopicEx(sName), msg.DstUser.UserId, int32(pb.MsgId_S2CChatId), msg)
|
||||
//default:
|
||||
// s.SendServiceMsg(service.TopicEx(topicName.WorldMessage), uid, int32(pb.MsgId_S2CChatId), msg)
|
||||
//}
|
||||
|
||||
}
|
98
test/client/server/service.go
Normal file
98
test/client/server/service.go
Normal file
@ -0,0 +1,98 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"game/common/proto/pb"
|
||||
"game/test/client/config"
|
||||
"github.com/fox/fox/ipb"
|
||||
"github.com/fox/fox/log"
|
||||
"github.com/fox/fox/processor"
|
||||
"github.com/fox/fox/service"
|
||||
"github.com/fox/fox/ws"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
var Chat []*ClientService
|
||||
|
||||
type ClientService struct {
|
||||
*service.BaseService
|
||||
client *ws.Client
|
||||
processor *processor.Processor
|
||||
}
|
||||
|
||||
func Init() {
|
||||
for i := 0; i < config.Command.ServiceNum; i++ {
|
||||
sid := config.Command.ServiceId + i
|
||||
if srv := newClientService(sid); srv != nil {
|
||||
Chat = append(Chat, srv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Stop() {
|
||||
for _, srv := range Chat {
|
||||
srv.NotifyStop()
|
||||
}
|
||||
for _, srv := range Chat {
|
||||
srv.WaitStop()
|
||||
}
|
||||
}
|
||||
|
||||
func newClientService(serviceId int) *ClientService {
|
||||
var err error
|
||||
s := new(ClientService)
|
||||
|
||||
sName := fmt.Sprintf("%v-%d", "client", serviceId)
|
||||
s.BaseService = service.NewBaseService("client", sName, s, s)
|
||||
size := len(config.Cfg.Special.Address)
|
||||
addr := config.Cfg.Special.Address[serviceId%size]
|
||||
if s.client, err = ws.NewClient(fmt.Sprintf("ws://%v", addr), s); err != nil {
|
||||
return nil
|
||||
}
|
||||
s.processor = processor.NewProcessor()
|
||||
s.initProcessor()
|
||||
s.OnInit()
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *ClientService) OnInit() {
|
||||
s.client.Start()
|
||||
s.BaseService.Run()
|
||||
log.Debug("onInit")
|
||||
}
|
||||
|
||||
func (s *ClientService) CanStop() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *ClientService) OnStop() {
|
||||
s.client.Stop()
|
||||
log.Debug("OnStop")
|
||||
}
|
||||
|
||||
// 处理其它服发送过来的消息
|
||||
func (s *ClientService) OnMessage(data []byte) error {
|
||||
var cMsg = &pb.ClientMsg{}
|
||||
var err error
|
||||
if err = proto.Unmarshal(data, cMsg); err != nil {
|
||||
log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
if req, err := s.processor.Unmarshal(cMsg.MsgId, cMsg.Data); err == nil {
|
||||
err = s.processor.Dispatch(cMsg.MsgId, cMsg.UserId, cMsg, req)
|
||||
}
|
||||
//log.Debug(s.Log("on message:%v", string(cMsg)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 向内部服务发送消息
|
||||
func (s *ClientService) SendServiceData(topic string, userId int64, msgId int32, data []byte) {
|
||||
iMsg := ipb.MakeMsg(s.Name(), 0, userId, msgId, data)
|
||||
_ = s.Send(topic, iMsg)
|
||||
}
|
||||
|
||||
// 向内部服务发送消息
|
||||
func (s *ClientService) SendServiceMsg(topic string, userId int64, msgId int32, msg proto.Message) {
|
||||
data, _ := proto.Marshal(msg)
|
||||
s.SendServiceData(topic, userId, msgId, data)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user