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.

92 lines
2.2 KiB

  1. /**
  2. * @Author: Aceld
  3. * @Date: 2019/4/30 17:42
  4. * @Mail: danbing.at@gmail.com
  5. * ZinxV0.11测试测试Zinx 日志模块功能 zlog模块
  6. */
  7. package main
  8. import (
  9. "github.com/aceld/zinx/ziface"
  10. "github.com/aceld/zinx/zlog"
  11. "github.com/aceld/zinx/znet"
  12. )
  13. //ping test 自定义路由
  14. type PingRouter struct {
  15. znet.BaseRouter
  16. }
  17. //Ping Handle
  18. func (this *PingRouter) Handle(request ziface.IRequest) {
  19. zlog.Debug("Call PingRouter Handle")
  20. //先读取客户端的数据,再回写ping...ping...ping
  21. zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
  22. err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping"))
  23. if err != nil {
  24. zlog.Error(err)
  25. }
  26. }
  27. type HelloZinxRouter struct {
  28. znet.BaseRouter
  29. }
  30. //HelloZinxRouter Handle
  31. func (this *HelloZinxRouter) Handle(request ziface.IRequest) {
  32. zlog.Debug("Call HelloZinxRouter Handle")
  33. //先读取客户端的数据,再回写ping...ping...ping
  34. zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
  35. err := request.GetConnection().SendBuffMsg(1, []byte("Hello Zinx Router V0.10"))
  36. if err != nil {
  37. zlog.Error(err)
  38. }
  39. }
  40. //创建连接的时候执行
  41. func DoConnectionBegin(conn ziface.IConnection) {
  42. zlog.Debug("DoConnecionBegin is Called ... ")
  43. //设置两个链接属性,在连接创建之后
  44. zlog.Debug("Set conn Name, Home done!")
  45. conn.SetProperty("Name", "Aceld")
  46. conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1")
  47. err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
  48. if err != nil {
  49. zlog.Error(err)
  50. }
  51. }
  52. //连接断开的时候执行
  53. func DoConnectionLost(conn ziface.IConnection) {
  54. //在连接销毁之前,查询conn的Name,Home属性
  55. if name, err := conn.GetProperty("Name"); err == nil {
  56. zlog.Error("Conn Property Name = ", name)
  57. }
  58. if home, err := conn.GetProperty("Home"); err == nil {
  59. zlog.Error("Conn Property Home = ", home)
  60. }
  61. zlog.Debug("DoConneciotnLost is Called ... ")
  62. }
  63. func main() {
  64. //创建一个server句柄
  65. s := znet.NewServer()
  66. //注册链接hook回调函数
  67. s.SetOnConnStart(DoConnectionBegin)
  68. s.SetOnConnStop(DoConnectionLost)
  69. //配置路由
  70. s.AddRouter(0, &PingRouter{})
  71. s.AddRouter(1, &HelloZinxRouter{})
  72. //开启服务
  73. s.Serve()
  74. }