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.

42 lines
846 B

  1. package main
  2. import (
  3. "fmt"
  4. "zinx/ziface"
  5. "zinx/znet"
  6. )
  7. //ping test 自定义路由
  8. type PingRouter struct {
  9. znet.BaseRouter
  10. }
  11. //Test Handle
  12. func (this *PingRouter) Handle(request ziface.IRequest) {
  13. fmt.Println("Call PingRouter Handle")
  14. //先读取客户端的数据,再回写ping...ping...ping
  15. fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
  16. //回写数据
  17. /*
  18. _, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n"))
  19. if err != nil {
  20. fmt.Println("call back ping ping ping error")
  21. }
  22. */
  23. err := request.GetConnection().SendMsg(1, []byte("ping...ping...ping"))
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. }
  28. func main() {
  29. //创建一个server句柄
  30. s := znet.NewServer()
  31. //配置路由
  32. s.AddRouter(&PingRouter{})
  33. //开启服务
  34. s.Serve()
  35. }