推薦項(xiàng)目目錄結(jié)構(gòu):
.
├── api 用戶自定義協(xié)議
│ └── test_api.go
├── client_walk.go 自動(dòng)化游戲全邏輯測(cè)試腳本
├── conf
│ └── server.json 服務(wù)器全局配置
├── core 游戲核心邏輯/各種游戲玩法,活動(dòng)相關(guān)邏輯放到這兒
│ ├── aoi.go
│ ├── player.go
│ └── worldmgr.go
├── log 默認(rèn)日志文件存放路徑/日志可按照時(shí)間和大小切割
│ ├── server.log
│ ├── server.log.2016-12-20
│ ├── server.log.2016-12-26
├── pb 消息定義
│ ├── msg.pb.go
│ └── msg.proto
├── README.md
├── server.go 服務(wù)器主邏輯
推薦clone demo項(xiàng)目到本地:
git clone https://git.oschina.net/viphxin/xingo_demo.git
配置服務(wù)器
vim conf/server.json
{ "TcpPort": 8999,//監(jiān)聽端口 "StepPerMs": 2000,//demo邏輯配置,非通用配置項(xiàng) "PoolSize": 5, //工作隊(duì)列數(shù) "IsUsePool": true,//是否使用工作隊(duì)列 "LogLevel": 1, //日志級(jí)別 0 ALL 1 DEBUG 2 INFO 3 WARN 4 ERROR 5 FATAL 6 OFF "MaxConn": 2000 //支持最大連接數(shù) }google protobuf消息定義:
message Talk{ string Content=1; }定義一個(gè)測(cè)試接口api,實(shí)現(xiàn)功能世界聊天(廣播每個(gè)鏈接發(fā)過來的talk消息):
導(dǎo)入xingo
import ( "xingo_demo/pb" "xingo_demo/core" "github.com/golang/protobuf/proto" "github.com/viphxin/xingo/fnet" "github.com/viphxin/xingo/logger" "fmt" )定義api接口:
type TestRouter struct { } /* ping test */ func (this *TestRouter) Api_0(request *fnet.PkgAll) { logger.Debug("call Api_0") // request.Fconn.SendBuff(0, nil) packdata, err := fnet.DefaultDataPack.Pack(0, nil) if err == nil{ request.Fconn.Send(packdata) }else{ logger.Error("pack data error") } } /* 世界聊天 */ func (this *TestRouter) Api_2(request *fnet.PkgAll) { msg := &pb.Talk{} err := proto.Unmarshal(request.Pdata.Data, msg)//解析Talk消息 if err == nil { logger.Debug(fmt.Sprintf("user talk: content: %s.", msg.Content)) pid, err1 := request.Fconn.GetProperty("pid") if err1 == nil{ p, _ := core.WorldMgrObj.GetPlayer(pid.(int32)) p.Talk(msg.Content)//廣播 }else{ logger.Error(err1) request.Fconn.LostConnection() } } else { logger.Error(err) request.Fconn.LostConnection() } }
實(shí)現(xiàn)自己的xingo server
package main import ( "fmt" "github.com/viphxin/xingo/fserver" "github.com/viphxin/xingo/iface" "github.com/viphxin/xingo/logger" "github.com/viphxin/xingo/utils" "xingo_demo/api" "xingo_demo/core" "os" "os/signal" ) func DoConnectionMade(fconn iface.Iconnection) { logger.Debug("111111111111111111111111") p, _ := core.WorldMgrObj.AddPlayer(fconn) fconn.SetProperty("pid", p.Pid) } func DoConnectionLost(fconn iface.Iconnection) { logger.Debug("222222222222222222222222") pid, _ := fconn.GetProperty("pid") p, _ := core.WorldMgrObj.GetPlayer(pid.(int32)) //移除玩家 core.WorldMgrObj.RemovePlayer(pid.(int32)) //消失在地圖 p.LostConnection() } func main() { s := fserver.NewServer() //add api ---------------start TestRouterObj := &api.TestRouter{} s.AddRouter(TestRouterObj)//注冊(cè)接口api //add api ---------------end //regest callback utils.GlobalObject.OnConnectioned = DoConnectionMade//綁定鏈接建立處理函數(shù) utils.GlobalObject.OnClosed = DoConnectionLost//綁定鏈接丟失處理函數(shù) s.Start()//開啟服務(wù)器 // close c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) sig := <-c fmt.Println("=======", sig) s.Stop() }
更多建議: