接下来我们在基于Zinx写服务器,就可以配置一个简单的路由功能了。

A) 测试基于Zinx完成的服务端应用

Server.go

  1. package main
  2. import (
  3. "fmt"
  4. "zinx/ziface"
  5. "zinx/znet"
  6. )
  7. //ping test 自定义路由
  8. type PingRouter struct {
  9. znet.BaseRouter //一定要先基础BaseRouter
  10. }
  11. //Test PreHandle
  12. func (this *PingRouter) PreHandle(request ziface.IRequest) {
  13. fmt.Println("Call Router PreHandle")
  14. _, err := request.GetConnection().GetTCPConnection().Write([]byte("before ping ....\n"))
  15. if err !=nil {
  16. fmt.Println("call back ping ping ping error")
  17. }
  18. }
  19. //Test Handle
  20. func (this *PingRouter) Handle(request ziface.IRequest) {
  21. fmt.Println("Call PingRouter Handle")
  22. _, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n"))
  23. if err !=nil {
  24. fmt.Println("call back ping ping ping error")
  25. }
  26. }
  27. //Test PostHandle
  28. func (this *PingRouter) PostHandle(request ziface.IRequest) {
  29. fmt.Println("Call Router PostHandle")
  30. _, err := request.GetConnection().GetTCPConnection().Write([]byte("After ping .....\n"))
  31. if err !=nil {
  32. fmt.Println("call back ping ping ping error")
  33. }
  34. }
  35. func main(){
  36. //创建一个server句柄
  37. s := znet.NewServer("[zinx V0.3]")
  38. s.AddRouter(&PingRouter{})
  39. //2 开启服务
  40. s.Serve()
  41. }

我们这里自定义了一个类似Ping操作的路由,就是当客户端发送数据,我们的处理业务就是返回给客户端”ping…ping..ping..”, 为了测试,当前路由也同时实现了PreHandle和PostHandle两个方法。实际上Zinx会利用模板的设计模式,依次在框架中调用PreHandleHandlePostHandle三个方法。

B) 启动Server.go
  1. go run Server.go

C) 客户端应用测试程序

和之前的Client.go一样 没有改变

  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. )
  7. /*
  8. 模拟客户端
  9. */
  10. func main() {
  11. fmt.Println("Client Test ... start")
  12. //3秒之后发起测试请求,给服务端开启服务的机会
  13. time.Sleep(3 * time.Second)
  14. conn,err := net.Dial("tcp", "127.0.0.1:7777")
  15. if err != nil {
  16. fmt.Println("client start err, exit!")
  17. return
  18. }
  19. for {
  20. _, err := conn.Write([]byte("Zinx V0.3"))
  21. if err !=nil {
  22. fmt.Println("write error err ", err)
  23. return
  24. }
  25. buf :=make([]byte, 512)
  26. cnt, err := conn.Read(buf)
  27. if err != nil {
  28. fmt.Println("read buf error ")
  29. return
  30. }
  31. fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt)
  32. time.Sleep(1*time.Second)
  33. }
  34. }

D) 启动Client.go
  1. go run Client.go

运行结果如下:

服务端:

  1. $ go run Server.go
  2. Add Router succ!
  3. [START] Server listenner at IP: 0.0.0.0, Port 7777, is starting
  4. start Zinx server [zinx V0.3] succ, now listenning...
  5. Reader Goroutine is running
  6. Call Router PreHandle
  7. Call PingRouter Handle
  8. Call Router PostHandle
  9. Call Router PreHandle
  10. Call PingRouter Handle
  11. Call Router PostHandle
  12. Call Router PreHandle
  13. Call PingRouter Handle
  14. Call Router PostHandle
  15. Call Router PreHandle
  16. Call PingRouter Handle
  17. Call Router PostHandle
  18. Call Router PreHandle
  19. Call PingRouter Handle
  20. Call Router PostHandle
  21. ...

客户端:

  1. $ go run Client.go
  2. Client Test ... start
  3. server call back : before ping ....
  4. , cnt = 17
  5. server call back : ping...ping...ping
  6. After ping .....
  7. , cnt = 36
  8. server call back : before ping ....
  9. ping...ping...ping
  10. After ping .....
  11. , cnt = 53
  12. server call back : before ping ....
  13. ping...ping...ping
  14. After ping .....
  15. , cnt = 53
  16. server call back : before ping ....
  17. ping...ping...ping
  18. After ping .....
  19. , cnt = 53
  20. ...

现在Zinx框架已经有路由功能了,虽然说目前只能配置一个,不过不要着急,很快我们会增加配置多路由的能力。