server.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //go:build ignore
  2. // +build ignore
  3. package main
  4. import (
  5. "errors"
  6. "log"
  7. "os"
  8. "os/signal"
  9. "regexp"
  10. "syscall"
  11. "time"
  12. "git.me9.top/git/tinymq"
  13. "git.me9.top/git/tinymq/config"
  14. )
  15. func main() {
  16. cf := config.NewConfig()
  17. localChannel := "/tinymq/server"
  18. remoteChannel := "/tinymq/client"
  19. var hub *tinymq.Hub
  20. hub = tinymq.NewHub(cf, localChannel,
  21. func(channel string) (hostInfo *tinymq.HostInfo, err error) {
  22. return nil, errors.New("not host found")
  23. }, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  24. // 从 remoteAuth 是否为空来判断是否需要返回信息
  25. if len(remoteAuth) <= 0 {
  26. // 客户端调用,返回验证信息
  27. return []byte("tinymq")
  28. } else {
  29. // 服务端调用,返回验证token,或者其他信息
  30. return nil
  31. }
  32. }, func(proto string, version uint8, channel string, auth []byte) bool {
  33. return string(auth) == "tinymq"
  34. }, func(conn *tinymq.Line) {
  35. log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Updated()))
  36. if conn.State() == tinymq.Connected {
  37. go hub.Get(regexp.MustCompile(remoteChannel), "hello", []byte("hello from server push"),
  38. func(response *tinymq.ResponseData) (ok bool) {
  39. log.Println("[hello response]", response.State, string(response.Data))
  40. return true
  41. })
  42. go hub.Get(regexp.MustCompile(remoteChannel), "nodata", nil,
  43. func(response *tinymq.ResponseData) (ok bool) {
  44. log.Println("[nodata response]", response.State, string(response.Data))
  45. return true
  46. })
  47. }
  48. },
  49. )
  50. // tcp2协议
  51. bindTpv2Info := &tinymq.HostInfo{
  52. Proto: "tcp",
  53. Version: 2,
  54. Bind: "127.0.0.1",
  55. Port: 34222,
  56. Hash: "xor:1qaz2wsx3",
  57. }
  58. hub.BindForServer(bindTpv2Info)
  59. // ws2协议
  60. bindws2Info := &tinymq.HostInfo{
  61. Proto: "ws",
  62. Version: 2,
  63. Bind: "127.0.0.1",
  64. Port: 34211,
  65. Path: "/tinymq-xor",
  66. Hash: "xor:1qaz2wsx3edc",
  67. }
  68. hub.BindForServer(bindws2Info)
  69. // ws2协议,没有加密算法
  70. bindInfo := &tinymq.HostInfo{
  71. Proto: "ws",
  72. Version: 2,
  73. // Bind: "127.0.0.1",
  74. Port: 34211,
  75. Path: "/tinymq",
  76. }
  77. hub.BindForServer(bindInfo)
  78. // 中间件
  79. hub.UseMiddle(func(request *tinymq.RequestData) (response *tinymq.ResponseData) {
  80. log.Println("[Middle]", request.Conn().Channel(), request.Cmd)
  81. return nil
  82. })
  83. // 订阅频道
  84. hub.Subscribe(regexp.MustCompile(remoteChannel), "hello",
  85. func(request *tinymq.RequestData) (state uint8, result []byte) {
  86. log.Println("[server RECV]<-", string(request.Data))
  87. return 1, []byte("tiny server")
  88. },
  89. )
  90. hub.Subscribe(regexp.MustCompile(remoteChannel), "nodata",
  91. func(request *tinymq.RequestData) (state uint8, result []byte) {
  92. log.Println("[server RECV]<-", string(request.Data))
  93. return 1, nil
  94. },
  95. )
  96. hub.Subscribe(regexp.MustCompile(remoteChannel), "push",
  97. func(request *tinymq.RequestData) (state uint8, result []byte) {
  98. log.Println("[server RECV]<-", string(request.Data))
  99. return 1, nil
  100. },
  101. )
  102. // log.Fatal(http.ListenAndServe(net.JoinHostPort(bindws2Info.Bind, strconv.Itoa(int(bindws2Info.Port))), nil))
  103. // 初始化一个channel
  104. exit := make(chan os.Signal, 3)
  105. //notify方法用来监听收到的信号
  106. signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
  107. sig := <-exit
  108. log.Println("[Exist with]", sig.String())
  109. }