run in terminal: go test -v ./znet -run=TestServer * ClientTest client */
(i uint32)
| 19 | ClientTest client |
| 20 | */ |
| 21 | func ClientTest(i uint32) { |
| 22 | |
| 23 | fmt.Println("Client Test ... start") |
| 24 | |
| 25 | //3秒之后发起测试请求,给服务端开启服务的机会 |
| 26 | time.Sleep(3 * time.Second) |
| 27 | |
| 28 | conn, err := net.Dial("tcp", "127.0.0.1:8999") |
| 29 | if err != nil { |
| 30 | fmt.Println("client start err, exit!") |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | for { |
| 35 | dp := zpack.Factory().NewPack(ziface.ZinxDataPack) |
| 36 | msg, _ := dp.Pack(zpack.NewMsgPackage(i, []byte("client test message"))) |
| 37 | _, err := conn.Write(msg) |
| 38 | if err != nil { |
| 39 | fmt.Println("client write err: ", err) |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | headData := make([]byte, dp.GetHeadLen()) |
| 44 | _, err = io.ReadFull(conn, headData) |
| 45 | if err != nil { |
| 46 | fmt.Println("client read head err: ", err) |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | msgHead, err := dp.Unpack(headData) |
| 51 | if err != nil { |
| 52 | fmt.Println("client unpack head err: ", err) |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | if msgHead.GetDataLen() > 0 { |
| 57 | msg := msgHead.(*zpack.Message) |
| 58 | msg.Data = make([]byte, msg.GetDataLen()) |
| 59 | |
| 60 | _, err := io.ReadFull(conn, msg.Data) |
| 61 | if err != nil { |
| 62 | fmt.Println("client unpack data err") |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | fmt.Printf("==> Client receive Msg: ID = %d, len = %d , data = %s\n", msg.ID, msg.DataLen, msg.Data) |
| 67 | } |
| 68 | |
| 69 | time.Sleep(time.Second) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | server |
no test coverage detected