50 lines
1.1 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aceld/zinx/ziface"
  5. "github.com/aceld/zinx/znet"
  6. )
  7. //ping test 自定义路由
  8. type PingRouter struct {
  9. znet.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()
  38. //配置路由
  39. s.AddRouter(&PingRouter{})
  40. //开启服务
  41. s.Serve()
  42. }