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.

59 lines
1.3 KiB

  1. /**
  2. * @Author: Aceld
  3. * @Date: 2020/12/24 00:24
  4. * @Mail: danbing.at@gmail.com
  5. * zinx server demo
  6. */
  7. package main
  8. import (
  9. "zinx/examples/zinx_server/zrouter"
  10. "zinx/ziface"
  11. "zinx/zlog"
  12. "zinx/znet"
  13. )
  14. //创建连接的时候执行
  15. func DoConnectionBegin(conn ziface.IConnection) {
  16. zlog.Debug("DoConnecionBegin is Called ... ")
  17. //设置两个链接属性,在连接创建之后
  18. zlog.Debug("Set conn Name, Home done!")
  19. conn.SetProperty("Name", "Aceld")
  20. conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1")
  21. err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
  22. if err != nil {
  23. zlog.Error(err)
  24. }
  25. }
  26. //连接断开的时候执行
  27. func DoConnectionLost(conn ziface.IConnection) {
  28. //在连接销毁之前,查询conn的Name,Home属性
  29. if name, err := conn.GetProperty("Name"); err == nil {
  30. zlog.Error("Conn Property Name = ", name)
  31. }
  32. if home, err := conn.GetProperty("Home"); err == nil {
  33. zlog.Error("Conn Property Home = ", home)
  34. }
  35. zlog.Debug("DoConneciotnLost is Called ... ")
  36. }
  37. func main() {
  38. //创建一个server句柄
  39. s := znet.NewServer()
  40. //注册链接hook回调函数
  41. s.SetOnConnStart(DoConnectionBegin)
  42. s.SetOnConnStop(DoConnectionLost)
  43. //配置路由
  44. s.AddRouter(0, &zrouter.PingRouter{})
  45. s.AddRouter(1, &zrouter.HelloZinxRouter{})
  46. //开启服务
  47. s.Serve()
  48. }