上传战斗模块远程连接对象封装

This commit is contained in:
liwei1dao 2022-11-24 11:06:10 +08:00
parent 3c6b2b12cd
commit a7b469b523
3 changed files with 97 additions and 1 deletions

40
modules/battle/client.go Normal file
View File

@ -0,0 +1,40 @@
package battle
import (
"go_dreamfactory/modules"
"go_dreamfactory/lego/core"
"github.com/gorilla/websocket"
)
/*
战斗服务客户端组件
*/
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() {
}

View File

@ -18,6 +18,8 @@ import (
"os"
"testing"
"time"
"github.com/gorilla/websocket"
)
func newService(ops ...rpcx.Option) core.IService {
@ -86,4 +88,13 @@ func testdefer(data *[]int) {
*data = append(*data, 0)
}
// 3
func Test_Comment(t *testing.T) {
dialer := websocket.Dialer{}
if connect, _, err := dialer.Dial("ws://127.0.0.1:9898", nil); err != nil {
fmt.Println(err)
return
} else {
defer connect.Close()
connect.WriteMessage(websocket.TextMessage, []byte("from client to server"))
}
}

45
modules/battle/options.go Normal file
View File

@ -0,0 +1,45 @@
package battle
import (
"errors"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/utils/mapstructure"
"go_dreamfactory/modules"
)
type (
IOptions interface {
modules.IOptions
}
Options struct {
modules.Options
BattleServerAddr string
}
)
func (this *Options) GetDebug() bool {
return this.Debug
}
func (this *Options) GetLog() log.ILogger {
return this.Log
}
func (this *Options) GetBattleServerAddr() string {
return this.BattleServerAddr
}
func (this *Options) LoadConfig(settings map[string]interface{}) (err error) {
if settings != nil {
if err = mapstructure.Decode(settings, &this.Options); err != nil {
return
}
if err = mapstructure.Decode(settings, this); err != nil {
return
}
}
if this.Log = log.NewTurnlog(true, log.Clone("", 4)); this.Log == nil {
err = errors.New("log is nil")
}
return
}