You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.0 KiB

  1. package znet
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "time"
  7. "zinx/ziface"
  8. )
  9. /*
  10. 模拟客户端
  11. */
  12. func ClientTest() {
  13. fmt.Println("Client Test ... start")
  14. //3秒之后发起测试请求,给服务端开启服务的机会
  15. time.Sleep(3 * time.Second)
  16. conn,err := net.Dial("tcp", "127.0.0.1:7777")
  17. if err != nil {
  18. fmt.Println("client start err, exit!")
  19. return
  20. }
  21. for {
  22. _, err := conn.Write([]byte("Zinx V0.2 test"))
  23. if err !=nil {
  24. fmt.Println("write error err ", err)
  25. return
  26. }
  27. buf :=make([]byte, 512)
  28. cnt, err := conn.Read(buf)
  29. if err != nil {
  30. fmt.Println("read buf error ")
  31. return
  32. }
  33. fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt)
  34. time.Sleep(1*time.Second)
  35. }
  36. }
  37. /*
  38. //Server 模块的测试函数
  39. func TestServer(t *testing.T) {
  40. // 服务端测试
  41. //1 创建一个server 句柄 s
  42. s := NewServer("[zinx V0.1]")
  43. // 客户端测试
  44. go ClientTest()
  45. //2 开启服务
  46. s.Serve()
  47. }
  48. */
  49. //ping test 自定义路由
  50. type PingRouter struct {
  51. BaseRouter
  52. }
  53. //Test PreHandle
  54. func (this *PingRouter) PreHandle(request ziface.IRequest) {
  55. fmt.Println("Call Router PreHandle")
  56. _, err := request.GetConnection().GetTCPConnection().Write([]byte("before ping ....\n"))
  57. if err !=nil {
  58. fmt.Println("call back ping ping ping error")
  59. }
  60. }
  61. //Test Handle
  62. func (this *PingRouter) Handle(request ziface.IRequest) {
  63. fmt.Println("Call PingRouter Handle")
  64. _, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n"))
  65. if err !=nil {
  66. fmt.Println("call back ping ping ping error")
  67. }
  68. }
  69. //Test PostHandle
  70. func (this *PingRouter) PostHandle(request ziface.IRequest) {
  71. fmt.Println("Call Router PostHandle")
  72. _, err := request.GetConnection().GetTCPConnection().Write([]byte("After ping .....\n"))
  73. if err !=nil {
  74. fmt.Println("call back ping ping ping error")
  75. }
  76. }
  77. func TestServerV0_3(t *testing.T){
  78. //创建一个server句柄
  79. s := NewServer("[zinx V0.3]")
  80. s.AddRouter(&PingRouter{})
  81. // 客户端测试
  82. go ClientTest()
  83. //2 开启服务
  84. s.Serve()
  85. }