package core import ( "fmt" "git.noahlan.cn/noahlan/nnet/config" "git.noahlan.cn/noahlan/nnet/entity" "git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/nnet/protocol" "git.noahlan.cn/noahlan/ntools-go/core/nlog" "git.noahlan.cn/noahlan/ntools-go/core/pool" "math" "testing" "time" ) func TestServer(t *testing.T) { server := NewServer(config.EngineConf{ ServerConf: config.ServerConf{ Protocol: "tcp", Addr: "0.0.0.0:6666", Name: "testServer", Mode: "dev", }, Pool: pool.Config{ PoolSize: math.MaxInt32, ExpiryDuration: time.Second, PreAlloc: false, MaxBlockingTasks: 0, Nonblocking: false, DisablePurge: false, }, }, protocol.WithNNetProtocol(protocol.NNetConfig{ HeartbeatInterval: 0, HandshakeValidator: nil, })...) server.AddRoute(Route{ Matches: protocol.Match{ Route: "test", Code: 1, }, Handler: func(entity entity.NetworkEntity, pkg packet.IPacket) { fmt.Println(pkg) p, ok := pkg.(*protocol.NNetPacket) if !ok { nlog.Error("wrong packet type") return } bd := []byte("服务器接收到数据为: " + string(p.GetBody())) // 注:Response类型数据不需要Route(原地返回,客户端需等待) _ = entity.Send(protocol.Header{ PacketType: protocol.Data, Length: uint32(len(bd)), MessageHeader: protocol.MessageHeader{ MsgType: protocol.Response, ID: p.ID, Route: p.Route, }, }, bd) }, }) defer server.Stop() server.Start() }