package core import ( "fmt" "git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/ntools-go/core/nlog" "testing" "time" ) func TestServer(t *testing.T) { server := NewServer(EngineConf{ Protocol: "tcp", Addr: ":12345", Name: "N-Net", Mode: DevMode, }, WithPacker(func() packet.Packer { return packet.NewNNetPacker() }), WithSerializer(nil), WithHeartbeatInterval(time.Hour), WithProcessor(NewNNetProcessor()), ) server.AddRoute(Route{ Matches: "test", Handler: func(conn *connection, pkg packet.IPacket) { fmt.Println(pkg) p, ok := pkg.(*packet.Packet) if !ok { nlog.Error("wrong packet type") return } bd := []byte("服务器接收到数据为: " + string(p.GetBody())) // 注:Response类型数据不需要Route(原地返回,客户端需等待) conn.Send(packet.Header{ PacketType: packet.Data, Length: uint32(len(bd)), MessageHeader: packet.MessageHeader{ MsgType: packet.Response, ID: p.ID, Route: p.Route, }, }, bd) }, }) defer server.Stop() server.Start() }