go_dreamfactory/modules/battle/client.go
2022-11-24 14:10:04 +08:00

62 lines
1.2 KiB
Go

package battle
import (
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/lego/core"
"github.com/gorilla/websocket"
"google.golang.org/protobuf/proto"
)
/*
战斗服务客户端组件
*/
type clientComp struct {
modules.MCompGate
options *Options
service core.IService
module *Battle
conn *websocket.Conn
}
//组件初始化接口
func (this *clientComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompGate.Init(service, module, comp, options)
this.options = options.(*Options)
this.module = module.(*Battle)
return
}
func (this *clientComp) Start() (err error) {
err = this.MCompGate.Start()
dialer := websocket.Dialer{}
this.conn, _, err = dialer.Dial(this.options.BattleServerAddr, nil)
return
}
//校验战斗过程
func (this *clientComp) CheckBattle() {
}
func (this *clientComp) run() {
var (
data []byte
msg *pb.BattleRpcMessage = &pb.BattleRpcMessage{}
err error
)
locp:
for {
if _, data, err = this.conn.ReadMessage(); err != nil {
this.module.Errorf("client err:%v", err)
break locp
}
if err = proto.Unmarshal(data, msg); err != nil {
this.module.Errorf("client Unmarshal err:%v", err)
break locp
}
}
}